First I would like to express my gratitude for this great book. It is a great way to study Rails.
Now, to the point. I scaffold the product and category. And for them I want to make a simple association many-to-one association. A product that belongs to a category. And so I state the association in the models and through a join model. Product.rd: ` has_many :categorizations has_many :categories, :through => :categorizations `
Catgorization.rb ( join table ): ` belongs_to :category belongs_to :product `
and Category.rb: ` has_many :products `
I’d like to be able to chose which category every product belongs to. I tried railscast episode 57 but somethings missing. I get an undefined method error if I use ` def category_id category.id if category end
def category_name=(id) self.category = Category.find_by_id(id) unless id.blank? end `
I load the categories with this (in the _form.html.erb, rendering the _form.html.erb in the edit.html.erb): `
<%= f.collection_select :category_id, Category.find(:all), :id, :name, :prompt => “Select a Category” %> or create one: <%= f.text_field :new_category_id %>
` The categories load as expected but when I chose one of them. Nothing’s happens (new_category_id works for creating a new category still doesn’t assign the product to the category).
And in the end I try to also display the category name in the index.html.erb with this: `
<%= product.category_name %></td> ` Again absolutely no luck. Is my association statement wrong, or the statement of category_id. Maybe something else. All the tutorials I found have them like that but for me it doesn’t work, What am I missing? I tried to be as detailed as possible. Thank you for your time and patience.