【发布时间】:2010-10-12 22:56:20
【问题描述】:
这是this 之前的问题,该问题已得到解答。我实际上发现我可以从该查询中删除一个连接,所以现在工作查询是
start_cards = DeckCard.find :all, :joins => [:card], :conditions => ["deck_cards.deck_id = ? and cards.start_card = ?", @game.deck.id, true]
这似乎有效。但是,当我尝试将这些 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
以及相关的模型(画面是桌上的玩家牌)
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,它并没有超出描述。
【问题讨论】:
标签: ruby-on-rails ruby activerecord join associations