这个问题是基于之前得到回答的问题。实际上我发现可以将查询中的一个join去掉,所以现在这个有效的查询语句是:
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
添加到玩家手牌中,这段代码运行良好。我想知道在DeckCard模型中是否需要belongs_to:tableau
,但是对于添加到玩家手牌中来说这很好用。我的DeckCard表中确实有tableau_id
和hand_id
列。
我在Rails API中查找了ReadOnlyRecord,除了描述之外,没有太多信息。