2

これを書く適切な方法はありますか、それとも間違っていますか? ネストされたインクルードを行う必要があります。このリンクを見つけましたが、機能していないようです。

def show
    @showring = Ring.includes(:stones => :upcharges, :variations).find(params[:id])
end

テーブルが 3 つあります... Rings which has_many stones Stones that has_many upcharges

モデル:

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
4

1 に答える 1

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 : a を宣言するhas_many :through

class Ring < ActiveRecord::Base
  has_many :stones
  has_many :upcharges, :through => :stones
end

次に、ビューで:

<%= @showring.upcharges.to_json.html_safe %>
于 2012-07-30T20:42:30.113 に答える