是什么原因造成这个的ActiveRecord :: ReadOnlyRecord错误? [英] What is causing this ActiveRecord::ReadOnlyRecord error?
问题描述
这是继<一href="http://stackoverflow.com/questions/628000/rails-whats-wrong-with-this-multiple-join-with-conditions-on-the-associations"相对=nofollow称号=此>这个的问题之前,这是回答。其实我发现我可以删除该查询联接,所以现在的工作查询
This follows this prior question, which was answered. I actually discovered I could remove a join from that query, so now the working query is
start_cards = DeckCard.find :all, :joins => [:card], :conditions => ["deck_cards.deck_id = ? and cards.start_card = ?", @game.deck.id, true]
这似乎工作。但是,当我尝试将这些DeckCards到另一个关联,我得到的ActiveRecord :: ReadOnlyRecord错误。
This appears to work. However, when I try to move these DeckCards into another association, I get the ActiveRecord::ReadOnlyRecord error.
这里的code
for player in @game.players
player.tableau = Tableau.new
start_card = start_cards.pop
start_card.draw_pile = false
player.tableau.deck_cards << start_card # the error occurs on this line
end
和相关模型(画面是餐桌上的球员卡)
and the relevant Models (tableau are the players cards on the table)
class Player < ActiveRecord::Base
belongs_to :game
belongs_to :user
has_one :hand
has_one :tableau
end
class Tableau < ActiveRecord::Base
belongs_to :player
has_many :deck_cards
end
class DeckCard < ActiveRecord::Base
belongs_to :card
belongs_to :deck
end
我做了类似的行动只是在此之后code,加入 DeckCards
的球员手中,而code是工作的罚款。我想,如果我需要 belongs_to的:在DeckCard模式画面
,但它工作正常的增加了玩家的手中。我有一个 tableau_id
和 hand_id
列在DeckCard表。
I am doing a similar action just after this code, adding DeckCards
to the players hand, and that code is working fine. I wondered if I needed belongs_to :tableau
in the DeckCard Model, but it works fine for the adding to player's hand. I do have a tableau_id
and hand_id
columns in the DeckCard table.
我抬头ReadOnlyRecord在轨API,它不说远远超出了说明。
I looked up ReadOnlyRecord in the rails api, and it doesn't say much beyond the description.
推荐答案
从的ActiveRecord 更新日志
(v1.12.0,2005年10月16号):
介绍只读记录。如果调用object.readonly!然后它会 将对象标记为只读和提高 如果调用ReadOnlyRecord object.save。 object.readonly?报告 对象是否是只读的。 传:只读=> true以任何 finder方法将标志着返回 记录为只读。 的:加入 选项现在意味着:只读,因此,如果 使用此选项,保存同一个 现在的记录将失效。使用的find_by_sql 要解决。
Introduce read-only records. If you call object.readonly! then it will mark the object as read-only and raise ReadOnlyRecord if you call object.save. object.readonly? reports whether the object is read-only. Passing :readonly => true to any finder method will mark returned records as read-only. The :joins option now implies :readonly, so if you use this option, saving the same record will now fail. Use find_by_sql to work around.
使用的find_by_sql
是不是一个真正的选择,因为它返回原行/列数据,而不是 ActiveRecords
。你有两个选择:
Using find_by_sql
is not really an alternative as it returns raw row/column data, not ActiveRecords
. You have two options:
- 在记录(黑客)强制实例变量
@ReadOnly
为false - 使用
:包括=&GT; :卡
而不是:加入=&GT; :卡
- Force the instance variable
@readonly
to false in the record (hack) - Use
:include => :card
instead of:join => :card
2010年9月更新
上述大部分不再成立。因此,在Rails的2.3.4的和的3.0.0:
Most of the above no longer holds true. Thus, in Rails 2.3.4 and 3.0.0:
- 使用
Record.find_by_sql
是一个可行的选择 -
:只读=&GT;真正的
自动推断的仅如果:加入
指定的不明确:选择
和明确(或取景范围的继承):只读
选项(参见实施在active_record / base.rb
Rails的2.3.4,或set_readonly_option! >在
active_record / relation.rb
和custom_join_sql
在> active_record /关系/ query_methods.rb
Rails的3.0.0) - 然而,
:只读=&GT;真正的
总是在has_and_belongs_to_many
自动推断,如果连接表已经超过了两个外键列和:加入
指定不明确的:选择
(即用户提供的:只读
值被忽略 - 看到finding_with_ambiguous_select?
在active_record /协会/ has_and_belongs_to_many_association.rb
) - 在最后,除非处理的是一个特殊的连接表和
has_and_belongs_to_many
,然后@aaronrustad
的回答只是适用罚款的Rails 2.3.4和3.0.0。 - 请的没有的使用
:包括
如果你想实现一个INNER JOIN
(:包括
意味着LEFT OUTER JOIN
,这是选择性较差,比低效率的INNER JOIN
)
- using
Record.find_by_sql
is a viable option :readonly => true
is automatically inferred only if:joins
was specified without an explicit:select
nor an explicit (or finder-scope-inherited):readonly
option (see the implementation ofset_readonly_option!
inactive_record/base.rb
for Rails 2.3.4, or the implementation ofto_a
inactive_record/relation.rb
and ofcustom_join_sql
inactive_record/relation/query_methods.rb
for Rails 3.0.0)- however,
:readonly => true
is always automatically inferred inhas_and_belongs_to_many
if the join table has more than the two foreign keys columns and:joins
was specified without an explicit:select
(i.e. user-supplied:readonly
values are ignored -- seefinding_with_ambiguous_select?
inactive_record/associations/has_and_belongs_to_many_association.rb
.) - in conclusion, unless dealing with a special join table and
has_and_belongs_to_many
, then@aaronrustad
's answer applies just fine in Rails 2.3.4 and 3.0.0. - do not use
:includes
if you want to achieve anINNER JOIN
(:includes
implies aLEFT OUTER JOIN
, which is less selective and less efficient thanINNER JOIN
.)
这篇关于是什么原因造成这个的ActiveRecord :: ReadOnlyRecord错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!