# Association problem

**URL:** <https://discuss.rubyonrails.org/t/association-problem/46080>\
**Category:** rubyonrails-talk\
**Created:** [September 15, 2009, 11:22am UTC](https://discuss.rubyonrails.org/t/association-problem/46080 "2009-09-15T11:22:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Doloo](https://avatars.discourse-cdn.com/v4/letter/d/57b2e6/32.png) [@Doloo](https://discuss.rubyonrails.org/u/Doloo)\
**Post date:** [September 15, 2009, 11:22am UTC](https://discuss.rubyonrails.org/t/association-problem/46080/1 "2009-09-15T11:22:40Z")

</div>

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 &nbsp;&nbsp;has\_many :product\_groups &nbsp;&nbsp;has\_many :products, :through=\>:product\_groups end

# product\_group.rb class ProductGroup \< ActiveRecord::Base &nbsp;&nbsp;has\_many :products &nbsp;&nbsp;belongs\_to :user end

# product.rb class Product \< ActiveRecord::Base &nbsp;&nbsp;belongs\_to :product\_group &nbsp;&nbsp;has\_one :user end

So when i trying add new product. I get errors. # products\_controller.rb &nbsp;&nbsp;def new &nbsp;&nbsp;&nbsp;&nbsp;@product = current\_user.product\_groups.products.build &nbsp;&nbsp;end The error I'm receiving is:

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

I'm confused, can anybody help me?

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [September 15, 2009, 12:25pm UTC](https://discuss.rubyonrails.org/t/association-problem/46080/2 "2009-09-15T12:25:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Doloo](https://avatars.discourse-cdn.com/v4/letter/d/57b2e6/32.png) [@Doloo](https://discuss.rubyonrails.org/u/Doloo)\
**Post date:** [September 15, 2009, 12:30pm UTC](https://discuss.rubyonrails.org/t/association-problem/46080/3 "2009-09-15T12:30:45Z")

</div>

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