1

楷模:

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

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

4

0 回答 0