在此之前的问题得到了回答。我实际上发现我可以从该查询中删除联接,所以现在可以使用的查询是
start_cards = DeckCard.find :all, :joins => [:card], :conditions => ["deck_cards.deck_id = ? and cards.start_card = ?", @game.deck.id, true]
这似乎起作用。但是,当我尝试将这些DeckCards移到另一个关联中时,出现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 :tableauDeckCard模型,但是它对于增加玩家的手部效果很好。我在DeckCard表中确实有tableau_id和hand_id列。
我在rails api中查找了ReadOnlyRecord,并没有太多描述。