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.
Michael J. (Guest)
on 2006-03-31 02: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 J.
removed_email_address@domain.invalid
Eric G. (Guest)
on 2006-03-31 02: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 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
>


--
Eric G.
http://www.ericgoodwin.com
Eric G. (Guest)
on 2006-03-31 02: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 G. wrote:
>
>> and it's own id element.
>> @group = Group.find(1)
>>
>
--
Eric G.
http://www.ericgoodwin.com
Michael J. (Guest)
on 2006-03-31 02:20
(Received via mailing list)
No dice, that didn't seem to work :-\

Michael J.
removed_email_address@domain.invalid
Michael J. (Guest)
on 2006-03-31 02:23
(Received via mailing list)
Yep, I tried both plural and non-plural forms.  Rails didn't like
either.

Michael J.
removed_email_address@domain.invalid
Josh S. (Guest)
on 2006-03-31 06:31
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
This topic is locked and can not be replied to.