这遵循了前面的问题,这个问题已经得到了回答。实际上我发现我可以从那个查询中删除一个连接,所以现在工作的查询是
start_cards = DeckCard.find :all, :joins => [:card], :conditions => ["deck_cards.deck_id = ? and cards.start_card = ?", @game.deck.id, true]
这似乎奏效了。然而,当我试图将这些deck移动到另一个关联时,我得到ActiveRecord::ReadOnlyRecord错误。
下面是代码
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
和相关的模型(tableau是桌子上的玩家卡)
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
我在这段代码之后做了类似的操作,将DeckCards添加到玩家手上,这段代码工作正常。我想知道我是否需要belongs_to:tableau在DeckCard模型,但它工作得很好,添加到玩家的手。我有一个tableau_id和hand_id列在DeckCard表。
我在rails api中查找了ReadOnlyRecord,除了描述之外,它并没有提供太多信息。
Rails 2.3.3及以下版本
摘自ActiveRecord CHANGELOG(v1.12.0, 2005年10月16日):
引入只读记录。如果你调用object.readonly!那么它就会
将对象标记为只读并引发
ReadOnlyRecord如果你呼叫
object.save。object.readonly吗?报告
对象是否只读。
传递:readonly => true to any
查找器方法将标记返回
记录为只读。:加入
选项现在暗示:readonly,所以如果
您使用此选项,保存相同的内容
记录现在将失败。使用find_by_sql
四处工作。
使用find_by_sql并不是一个真正的替代方案,因为它返回原始的行/列数据,而不是ActiveRecords。你有两个选择:
在记录中强制实例变量@readonly为false (hack)
使用:include =>:card代替:join =>:card
Rails 2.3.4及以上版本
在2012年9月10日之后,上述大部分都不再成立:
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 of set_readonly_option! in active_record/base.rb for Rails 2.3.4, or the implementation of to_a in active_record/relation.rb and of custom_join_sql in active_record/relation/query_methods.rb for Rails 3.0.0)
however, :readonly => true is always automatically inferred in has_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 -- see finding_with_ambiguous_select? in active_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 an INNER JOIN (:includes implies a LEFT OUTER JOIN, which is less selective and less efficient than INNER JOIN.)
Rails 2.3.3及以下版本
摘自ActiveRecord CHANGELOG(v1.12.0, 2005年10月16日):
引入只读记录。如果你调用object.readonly!那么它就会
将对象标记为只读并引发
ReadOnlyRecord如果你呼叫
object.save。object.readonly吗?报告
对象是否只读。
传递:readonly => true to any
查找器方法将标记返回
记录为只读。:加入
选项现在暗示:readonly,所以如果
您使用此选项,保存相同的内容
记录现在将失败。使用find_by_sql
四处工作。
使用find_by_sql并不是一个真正的替代方案,因为它返回原始的行/列数据,而不是ActiveRecords。你有两个选择:
在记录中强制实例变量@readonly为false (hack)
使用:include =>:card代替:join =>:card
Rails 2.3.4及以上版本
在2012年9月10日之后,上述大部分都不再成立:
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 of set_readonly_option! in active_record/base.rb for Rails 2.3.4, or the implementation of to_a in active_record/relation.rb and of custom_join_sql in active_record/relation/query_methods.rb for Rails 3.0.0)
however, :readonly => true is always automatically inferred in has_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 -- see finding_with_ambiguous_select? in active_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 an INNER JOIN (:includes implies a LEFT OUTER JOIN, which is less selective and less efficient than INNER JOIN.)