ruby on rails - HABTM with extra attributes on nested form -
i have products have , belong many product_links
class product < activerecord::base has_many :map_product_prices has_many :product_links, :through => :map_product_prices accepts_nested_attributes_for :product_links
and...
class productlink < activerecord::base has_many :map_product_prices has_many :products, :through => :map_product_prices accepts_nested_attributes_for :products
i using ryan bates 'nested_form' gem this. here problem cannot wrap brain around. map table has price attribute attached also.
class mapproductprice < activerecord::base attr_accessible :product_link_id, :product_id, :price belongs_to :product belongs_to :product_link
i had multi-select box working select 1 or many products while on product link form. when switched on ryans gem use nested forms have single select box choose product, text field input price. can add / delete them necessary.
here i've got in view:
<%= f.fields_for :products |product| %> <%= product.select :product_id, options_for_select(select_options) %> <%= product.text_field :price %> <%= product.link_to_remove "remove product" %> <% end %> <p><%= f.link_to_add "add product", :products %></p>
anyone have ideas on how best accomplish this?
what i'm getting form there no product_id attribute product. makes sense because product_id on mapping table, not product. how can reference mapping table here in nested form instead of product? don't want able add new products, want able add new mappings existing products addition of price next each mapping.
thanks
edit: have edited code reflect has many through tried. when @ view, tells me there no product_id or price attribute on product builder object. how translate design onto form , make work? thanks
you need switch habtm has_many :through
relationship. habtm table can have 2 foreign key fields, has_many :through
join model has primary key, , can can add many fields want join model.
rails guide: here
a example of when want use has_many : through
relationship join model adding exercises workout:
class workout < activerecord::base has_many :workout_exercises, has_many :exercises, :through => :workout_exercises class workoutexercise < activerecord::base belongs_to :exercise belongs_to :workout class exercise < activerecord::base has_many :workout_exercises has_many :workouts, :through => :workout_exercises
so exercise within workout can have weight, or whatever else want associated adding weight field join table.
Comments
Post a Comment