# Has\_many :through problems -- please help

**URL:** <https://www.ruby-forum.com/t/has-many-through-problems-please-help/56405>\
**Category:** Rails\
**Created:** [March 31, 2006, 12:05am UTC](https://www.ruby-forum.com/t/has-many-through-problems-please-help/56405 "2006-03-31T00:05:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bluetrust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@bluetrust](https://www.ruby-forum.com/u/bluetrust)\
**Post date:** [March 31, 2006, 12:05am UTC](https://www.ruby-forum.com/t/has-many-through-problems-please-help/56405/1 "2006-03-31T00:05:07Z")

</div>

I’m having a lot of trouble with has\_many, :through and could really  
use some assistance.

I’ve got a User and Group class. Users can subscribe to groups, and  
groups should know who’s subscribed.

I’ve got a join table (group\_subscriptions) with group\_id, user\_id,  
and it’s own id element.

Users has “has\_many :groups, :through =\> :group\_subscription” and  
Groups has “has\_many :users, :through =\> :group\_subscription”. I  
also have a group\_subscription.rb model that has “belongs\_to :user  
_newline_ belongs\_to :group”

I just wanted to test out working with this kind of functionality, so  
I’ve got this simple code:

@user = User.find(1)  
@group = Group.find(1)  
@user.groups \<\< @group  
@user.save

but I get the error  
“ActiveRecord::HasManyThroughAssociationNotFoundError”. I’m running  
Webrick on OS X, have restarted it, and have looked everywhere to no  
avail in this problem.

Michael J.  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![bluetrust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@bluetrust](https://www.ruby-forum.com/u/bluetrust)\
**Post date:** [March 31, 2006, 12:11am UTC](https://www.ruby-forum.com/t/has-many-through-problems-please-help/56405/2 "2006-03-31T00:11:14Z")

</div>

I haven’t tested this out but I suspect that this should work. This is  
what I have and it works fine.

User.rb  
has\_many :group\_subscription  
has\_many :groups, :through =\> :group\_subscription

Group.rb  
has\_many :group\_subscription  
has\_many :users, :through =\> :group\_subscription

-Eric

Michael J. wrote:

> Groups has “has\_many :users, :through =\> :group\_subscription”. I also
> 
> * * *
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Eric G.  
[http://www.ericgoodwin.com](http://www.ericgoodwin.com)

---

<div class="post-metadata">

**Author:** ![bluetrust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@bluetrust](https://www.ruby-forum.com/u/bluetrust)\
**Post date:** [March 31, 2006, 12:17am UTC](https://www.ruby-forum.com/t/has-many-through-problems-please-help/56405/3 "2006-03-31T00:17:22Z")

</div>

Oops,  
Forgot the plural.

User.rb  
has\_many :group\_subscriptions  
has\_many :groups, :through =\> :group\_subscriptions

Group.rb  
has\_many :group\_subscriptions  
has\_many :users, :through =\> :group\_subscriptions

-Eric

Eric G. wrote:

> > and it’s own id element.  
> > @group = Group.find(1)

–  
Eric G.  
[http://www.ericgoodwin.com](http://www.ericgoodwin.com)

---

<div class="post-metadata">

**Author:** ![bluetrust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@bluetrust](https://www.ruby-forum.com/u/bluetrust)\
**Post date:** [March 31, 2006, 12:23am UTC](https://www.ruby-forum.com/t/has-many-through-problems-please-help/56405/4 "2006-03-31T00:23:31Z")

</div>

Yep, I tried both plural and non-plural forms. Rails didn’t like  
either.

Michael J.  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![bluetrust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@bluetrust](https://www.ruby-forum.com/u/bluetrust)\
**Post date:** [March 31, 2006, 12:20am UTC](https://www.ruby-forum.com/t/has-many-through-problems-please-help/56405/5 "2006-03-31T00:20:24Z")

</div>

No dice, that didn’t seem to work :-\

Michael J.  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![bluetrust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@bluetrust](https://www.ruby-forum.com/u/bluetrust)\
**Post date:** [March 31, 2006, 4:31am UTC](https://www.ruby-forum.com/t/has-many-through-problems-please-help/56405/6 "2006-03-31T04:31:40Z")

</div>

Michael J. wrote:

> Yep, I tried both plural and non-plural forms. Rails didn’t like  
> either.
> 
> Michael J.  
> removed\_email\_address@domain.invalid

You also need to have a class for the join model. In  
group\_subcription.rb:

class GroupSubscription \< AR::Base  
belongs\_to :user  
belongs\_to :group  
end

I have some examples on my blog.

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)
