通过连接表上的额外字段分配has_many

时间:2013-08-22 05:30:26

标签: ruby-on-rails ruby has-many-through

型号:

class MenuItem < ActiveRecord::Base
  has_many :deals, through: :deal_items
end

class DealItem < ActiveRecord::Base
  belongs_to :menu_item
  belongs_to :deal

  # has table column "primary"
end

class Deal < ActiveRecord::Base
  has_many :menu_items, through: :deal_items
end

控制器:

def create
  @deal = Deal.new(deal_params)

  params[:menu_items].each do |id|
    menu_item = MenuItem.find(id)
    @deal.menu_items << menu_item
    # the primary field on the DealItem object never got assigned!
  end

  if @deal.save
    render :index
  else
    render :new
  end
end

如何在MenuItem与交易相关联之前或之后分配primary列的值?

0 个答案:

没有答案