Wednesday, 21 August 2013

assigning has_many through extra fields on join table

assigning has_many through extra fields on join table

Models:
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
controller:
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
How do I assign the primary column's value either before or after the
MenuItem is associated with the deal?

No comments:

Post a Comment