I have a products that have and belong to 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 am using Ryan Bates 'nested_form' gem for this. Here is the problem I cannot wrap my brain around. The map table has a price attribute attached to it also.
class MapProductPrice < ActiveRecord::Base
attr_accessible :product_link_id, :product_id, :price
belongs_to :product
belongs_to :product_link
I had a multi-select box working to where I could select one or many products while on the product link form. But that is when I switched over to Ryans gem to use nested forms so that I could have a single select box to choose the product, then a text field to input price. Then I can add / delete them as necessary.
Here is what i've got in my view:
<%= f.fields_for :products do |product| %>
<%= product.select :product_id, options_for_select(select_options) %>
<%= product.text_field :price %>
<%= product.link_to_remove "Remove this product" %>
<% end %>
<p><%= f.link_to_add "Add a product", :products %></p>
Anyone have any ideas on how to best accomplish this?
what i'm getting from this form is that there is no product_id attribute for product. Which makes sense because product_id is on the mapping table, not the product. So how can I reference the mapping table here in this nested form instead of the product? I don't want to be able to add new products, I want to be able to add new mappings to existing products with the addition of a price next to each mapping.
Thanks
EDIT: I have edited my code to reflect the has many through that I tried. When I look at the view, it tells me that there is no product_id or price attribute on the product builder object. How to I translate this design onto the form and make it work? Thanks