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 -------------- next part -------------- A non-text attachment was scrubbed... Name: smime.p7s Type: application/pkcs7-signature Size: 2413 bytes Desc: not available Url : http://wrath.rubyonrails.org/pipermail/rails/attachments/20060330/f2c568a8/smime.bin
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:> 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 > > > > ------------------------------------------------------------------------ > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Eric Goodwin http://www.ericgoodwin.com
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:> 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: >> 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 >> >> >> >> ------------------------------------------------------------------------ >> >> _______________________________________________ >> Rails mailing list >> Rails@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > >-- Eric Goodwin http://www.ericgoodwin.com
Yep, I tried both plural and non-plural forms. Rails didn''t like either. Michael Jurewitz sinjin5@mac.com On Mar 30, 2006, at 4:20 PM, Eric Goodwin wrote:> 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: >> 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: >>> 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 >>> >>> >>> >>> -------------------------------------------------------------------- >>> ---- >>> >>> _______________________________________________ >>> Rails mailing list >>> Rails@lists.rubyonrails.org >>> http://lists.rubyonrails.org/mailman/listinfo/rails >>> >> >> > > > -- > Eric Goodwin > http://www.ericgoodwin.com > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails-------------- next part -------------- A non-text attachment was scrubbed... Name: smime.p7s Type: application/pkcs7-signature Size: 2413 bytes Desc: not available Url : http://wrath.rubyonrails.org/pipermail/rails/attachments/20060330/dc037339/smime.bin
No dice, that didn''t seem to work :-\ Michael Jurewitz sinjin5@mac.com On Mar 30, 2006, at 4:16 PM, Eric Goodwin wrote:> 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: >> 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 >> >> >> >> --------------------------------------------------------------------- >> --- >> >> _______________________________________________ >> Rails mailing list >> Rails@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > > > -- > Eric Goodwin > http://www.ericgoodwin.com > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails-------------- next part -------------- A non-text attachment was scrubbed... Name: smime.p7s Type: application/pkcs7-signature Size: 2413 bytes Desc: not available Url : http://wrath.rubyonrails.org/pipermail/rails/attachments/20060330/a6405994/smime.bin
Michael Jurewitz wrote:> Yep, I tried both plural and non-plural forms. Rails didn''t like > either. > > Michael Jurewitz > sinjin5@mac.comYou 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 -- Posted via http://www.ruby-forum.com/.