有没有正确的方法来写这个,或者我是否接近这个错误?我需要做一个嵌套的include。我找到了This Link,但似乎没有用。
def show
@showring = Ring.includes(:stones => :upcharges, :variations).find(params[:id])
end
我有3张桌子...... 有许多石头的戒指 有很多人收费的石头
型号:
class Ring < ActiveRecord::Base
has_many :stones
end
class Stone < ActiveRecord::Base
has_many :upcharges
belongs_to :ring
end
class Upcharge < ActiveRecord::Base
belongs_to :stone
end
答案 0 :(得分:7)
def show
@showring = Ring.includes([{:stones => :upcharges}, :variations]).find(params[:id])
end
添加了一些括号:)
获得所有收费:
@showring.stones.each do |s|
s.upcharges #Do whatever you need with it
end
选项2:声明has_many :through
class Ring < ActiveRecord::Base
has_many :stones
has_many :upcharges, :through => :stones
end
然后在视图中:
<%= @showring.upcharges.to_json.html_safe %>