相关疑难解决方法(0)

导致此ActiveRecord :: ReadOnlyRecord错误的原因是什么?

这是在这个先前的问题之后得到回答的.我实际上发现我可以从该查询中删除一个连接,所以现在工作查询是

start_cards = DeckCard.find :all, :joins => [:card], :conditions => ["deck_cards.deck_id = ? and cards.start_card = ?", @game.deck.id, true]  
Run Code Online (Sandbox Code Playgroud)

这似乎有效.但是,当我尝试将这些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
Run Code Online (Sandbox Code Playgroud)

和相关的模特(画面是桌上的球员卡)

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 …
Run Code Online (Sandbox Code Playgroud)

ruby activerecord join ruby-on-rails associations

203
推荐指数
5
解决办法
7万
查看次数

为什么使用连接时ARel范围变为只读?

如果您joins在ARel范围内使用,则结果将变为只读(即您无法更新您获得的任何记录).如果您不希望结果是只读的,那么您只需链接readonly(false)到范围,例如

User.joins(:orders).where(:orders => { :state => 'completed' }).readonly(false)

但我猜测默认的连接范围是只读的有一个原因.将结果设置为只读的原因是什么?

activerecord arel

11
推荐指数
1
解决办法
1546
查看次数

标签 统计

activerecord ×2

arel ×1

associations ×1

join ×1

ruby ×1

ruby-on-rails ×1