Forum: Ruby on Rails Has_many :through problems -- please help

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E4e7d8a7b40e4c791c9263e42b8cc9a3?d=identicon&s=25 Michael Jurewitz (Guest)
on 2006-03-31 00:05
(Received via mailing list)
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 Jurewitz
sinjin5@mac.com
Ac1da5d66d51a1c3b843221032247b30?d=identicon&s=25 Eric Goodwin (Guest)
on 2006-03-31 00:11
(Received via mailing list)
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 Jurewitz wrote:
> Groups has "has_many :users, :through => :group_subscription".  I also
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
Eric Goodwin
http://www.ericgoodwin.com
Ac1da5d66d51a1c3b843221032247b30?d=identicon&s=25 Eric Goodwin (Guest)
on 2006-03-31 00:17
(Received via mailing list)
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 Goodwin wrote:
>
>> and it's own id element.
>> @group = Group.find(1)
>>
>
--
Eric Goodwin
http://www.ericgoodwin.com
E4e7d8a7b40e4c791c9263e42b8cc9a3?d=identicon&s=25 Michael Jurewitz (Guest)
on 2006-03-31 00:20
(Received via mailing list)
No dice, that didn't seem to work :-\

Michael Jurewitz
sinjin5@mac.com
E4e7d8a7b40e4c791c9263e42b8cc9a3?d=identicon&s=25 Michael Jurewitz (Guest)
on 2006-03-31 00:23
(Received via mailing list)
Yep, I tried both plural and non-plural forms.  Rails didn't like
either.

Michael Jurewitz
sinjin5@mac.com
9f0f89bbd9e1ecfbaab6584e429b7a2f?d=identicon&s=25 Josh Susser (jsusser)
on 2006-03-31 04:31
Michael Jurewitz wrote:
> Yep, I tried both plural and non-plural forms.  Rails didn't like
> either.
>
> Michael Jurewitz
> sinjin5@mac.com

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 Susser
http://blog.hasmanythrough.com
This topic is locked and can not be replied to.