0

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

Sean
  • 1,078
  • 14
  • 25

1 Answers1

0

You need to switch from HABTM to a has_many :through relationship. A HABTM table can only have the two foreign key fields, a has_many :through join model has a primary key, and you can can add as many fields as you want to the join model.

Rails Guide: here

A good example of when you would want to use a has_many : throughrelationship for your join model is for something like adding exercises to a 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 now an exercise within a workout can have weight, or whatever else you want associated with it by just adding a weight field to the join table.

Arel
  • 3,888
  • 6
  • 37
  • 91
  • Thanks, I actually tried this earlier with the same results when it comes to the form submission. There is something i'm missing I guess.. – Sean Jul 17 '13 at 20:23
  • Any ideas..? Thanks for the help, i'm getting frustrated with this seemingly simple thing.. – Sean Jul 18 '13 at 16:28
  • I'm not really familiar with that gem. I'll take a look tonight if you still need the help. – Arel Jul 19 '13 at 15:53