Association problem

Hi, I’m new to ruby on rails. So i think i have association problems.

Given the three model classes with their associations:

user.rb

class User < ActiveRecord::Base
has_many :product_groups
has_many :products, :through=>:product_groups
end

product_group.rb

class ProductGroup < ActiveRecord::Base
has_many :products
belongs_to :user
end

product.rb

class Product < ActiveRecord::Base
belongs_to :product_group
has_one :user
end

So when i trying add new product. I get errors.

products_controller.rb

def new
@product = current_user.product_groups.products.build
end
The error I’m receiving is:

NoMethodError (undefined method products' for #<Class:0x2ca50b0>): app/controllers/products_controller.rb:27:innew’
-e:2:in `load’
-e:2

I’m confused, can anybody help me?

On Sep 15, 12:22 pm, Doloo [email protected] wrote:

 -e:2

A collection of product_groups doesn’t have a products method
(individual product group do).
Users have a products association, so why not do
current_user.products.build ? (Although I’m not sure if you can do a
has_many through where the join model has a has_many)

Fred

This is a example. I solve it. Thank you.

On Sep 15, 9:25 pm, Frederick C. [email protected]