Association problem in Rails
I am new to rubies on rails. So I think I am having association problems.
Considering three classes of models 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 try to add a new product. I am getting errors.
# products_controller.rb
def new
@product = current_user.product_groups.products.build
end
I am getting the following errors:
NoMethodError (undefined method `products' for #<Class:0x2ca50b0>):
app/controllers/products_controller.rb:27:in `new'
-e:2:in `load'
-e:2
I'm confused, can someone help me? Or any other idea?
+2
source to share