这是之前this个问题的答案.事实上,我发现我可以从该查询中删除连接,所以现在工作查询是
start_cards = DeckCard.find :all, :joins => [:card], :conditions => ["deck_cards.deck_id = ? and cards.start_card = ?", @game.deck.id, true]
这似乎奏效了.然而,当我try 将这些DeckCard移动到另一个关联中时,我会遇到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,它没有超出描述的内容.