So I''m one of those nasty people building a self-referential habtm-like Association using the funky new :through stuff. This is about users having friends, so here''s my user.rb: class User < ActiveRecord::Base has_many :friendships, :foreign_key => ''user_id'' has_many :friends, :through => :friendships, :source => :friend end And here''s my friendship.rb: class Friendship < ActiveRecord::Base belongs_to :user belongs_to :friend, :class_name => ''User'', :foreign_key => ''friend_id'' end Now, everything is working great as long as I create instances of Friendship "manually". Really no problems whatsoever, everything is working just perfectly. However, simply moving one user into another user''s list of friends does *not* seem to work: someone.friends << someoneelse # nothing happens Considering I''m not seeing any notes about collection.<< not working with :through associations, I''m wondering if this is specific to crazy self-referential stuff like what I''m doing. Any hints? Danke! -- http://www.mans.de
Hendrik Mans wrote:> So I''m one of those nasty people building a self-referential > habtm-like Association using the funky new :through stuff. This is > about users having friends, so here''s my user.rb: > > class User < ActiveRecord::Base > has_many :friendships, :foreign_key => ''user_id'' > has_many :friends, :through => :friendships, :source => :friend > end > > And here''s my friendship.rb: > > class Friendship < ActiveRecord::Base > belongs_to :user > belongs_to :friend, :class_name => ''User'', :foreign_key => ''friend_id'' > end > > Now, everything is working great as long as I create instances of > Friendship "manually". Really no problems whatsoever, everything is > working just perfectly. > > However, simply moving one user into another user''s list of friends > does *not* seem to work: > > someone.friends << someoneelse # nothing happens > > Considering I''m not seeing any notes about collection.<< not working > with :through associations, I''m wondering if this is specific to crazy > self-referential stuff like what I''m doing. > > Any hints?I don''t think you can create a join model record automatically by adding to a :through association. You have to create the friendship manually. When you think about it, that makes sense, since the main point of using a join model is to put other attributes in it. There''s nowhere to specify those attributes the way you''re trying to do it. Try doing someone.friendships.create(:friend => friend_user) and see if that works. -- Josh Susser http://blog.hasmanythrough.com -- Posted via http://www.ruby-forum.com/.
On 3/31/06, Josh Susser <josh@hasmanythrough.com> wrote:> Try doing someone.friendships.create(:friend => friend_user) and see if > that works.Yes, that works. I was just confused by the fact that there was no mention of << not working with :through associations.> When you think about it, that makes sense, since the main point of using > a join model is to put other attributes in it. There''s nowhere to > specify those attributes the way you''re trying to do it.Yes, I generally agree. However, using :through already has a bunch of advantages over simple habtm even without "rich" attributes that I would want to fill manually. In this scenario, the only extra attribute that I want is created_at, which is set automatically. I was assuming that AR would create the association model instance automatically, considering it could easy infer the class name etc. from the given parameters. Anyway, user.friendships.create() will work, I was just wondering really. Thanks, Hendrik -- http://www.mans.de
Hi Hendrik, as far as I know, :through associations are read-only. It makes sense because the model you are going :through is completely standalone and as such, it may have more than just the two belongs_to associations you specify. I.e. the has_many :through directive doesn''t capture enough information to reliably create the model you are going :through. Makes sense? -- Trevor Squires http://somethinglearned.com On 31-Mar-06, at 10:41 AM, Hendrik Mans wrote:> So I''m one of those nasty people building a self-referential > habtm-like Association using the funky new :through stuff. This is > about users having friends, so here''s my user.rb: > > class User < ActiveRecord::Base > has_many :friendships, :foreign_key => ''user_id'' > has_many :friends, :through => :friendships, :source => :friend > end > > And here''s my friendship.rb: > > class Friendship < ActiveRecord::Base > belongs_to :user > belongs_to :friend, :class_name => ''User'', :foreign_key => > ''friend_id'' > end > > Now, everything is working great as long as I create instances of > Friendship "manually". Really no problems whatsoever, everything is > working just perfectly. > > However, simply moving one user into another user''s list of friends > does *not* seem to work: > > someone.friends << someoneelse # nothing happens > > Considering I''m not seeing any notes about collection.<< not working > with :through associations, I''m wondering if this is specific to crazy > self-referential stuff like what I''m doing. > > Any hints? > > Danke! > > -- > http://www.mans.de > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Lets say your association table has a position field -- and you need to order by the position and you also need to be able to update the position hows that handled? Chris On Mar 31, 2006, at 12:38 PM, Trevor Squires wrote:> Hi Hendrik, > > as far as I know, :through associations are read-only. > > It makes sense because the model you are going :through is > completely standalone and as such, it may have more than > just the two belongs_to associations you specify. > > I.e. the has_many :through directive doesn''t capture enough > information > to reliably create the model you are going :through. > > Makes sense? > -- > Trevor Squires > http://somethinglearned.com > > > > On 31-Mar-06, at 10:41 AM, Hendrik Mans wrote: > >> So I''m one of those nasty people building a self-referential >> habtm-like Association using the funky new :through stuff. This is >> about users having friends, so here''s my user.rb: >> >> class User < ActiveRecord::Base >> has_many :friendships, :foreign_key => ''user_id'' >> has_many :friends, :through => :friendships, :source => :friend >> end >> >> And here''s my friendship.rb: >> >> class Friendship < ActiveRecord::Base >> belongs_to :user >> belongs_to :friend, :class_name => ''User'', :foreign_key => >> ''friend_id'' >> end >> >> Now, everything is working great as long as I create instances of >> Friendship "manually". Really no problems whatsoever, everything is >> working just perfectly. >> >> However, simply moving one user into another user''s list of friends >> does *not* seem to work: >> >> someone.friends << someoneelse # nothing happens >> >> Considering I''m not seeing any notes about collection.<< not working >> with :through associations, I''m wondering if this is specific to >> crazy >> self-referential stuff like what I''m doing. >> >> Any hints? >> >> Danke! >> >> -- >> http://www.mans.de >> _______________________________________________ >> Rails mailing list >> Rails@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Hendrik, Out of curiosity, what does your schema look like for Friendships? Hendrik Mans <hendrik@mans.de> wrote: So I''m one of those nasty people building a self-referential habtm-like Association using the funky new :through stuff. This is about users having friends, so here''s my user.rb: class User < ActiveRecord::Base has_many :friendships, :foreign_key => ''user_id'' has_many :friends, :through => :friendships, :source => :friend end And here''s my friendship.rb: class Friendship < ActiveRecord::Base belongs_to :user belongs_to :friend, :class_name => ''User'', :foreign_key => ''friend_id'' end Now, everything is working great as long as I create instances of Friendship "manually". Really no problems whatsoever, everything is working just perfectly. However, simply moving one user into another user''s list of friends does *not* seem to work: someone.friends << someoneelse # nothing happens Considering I''m not seeing any notes about collection.<< not working with :through associations, I''m wondering if this is specific to crazy self-referential stuff like what I''m doing. Any hints? Danke! -- http://www.mans.de _______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails --------------------------------- How low will we go? Check out Yahoo! Messenger?s low PC-to-Phone call rates. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060406/84dd7263/attachment.html
It''s not specific to what you''re doing... I had the same problem. I may have this wrong, but I think :through associations are strictly read-only. What I''d do: someone.friendships.create(:friend => someone_else) This is a little more pedantic, but closer to what''s happening in the database and perhaps your Friendship model: when you add someone to your list of friends, you''re creating a field in the friendships table. HABTM associations might allow you to use the line I''ve quoted? -- Michael Daines On Apr 5, 2006, at 7:17 PM, Wolfgang Borgon wrote:> However, simply moving one user into another user''s list of friends > does *not* seem to work: > > someone.friends << someoneelse # nothing happens > > Considering I''m not seeing any notes about collection.<< not working > with :through associations, I''m wondering if this is specific to crazy > self-referential stuff like what I''m doing.
Apparently Analagous Threads
- Self-referential has_many :through relationship
- How to access attribute in a self-referential many-to-many relationship
- Pagination has many through problems
- Self-Referential has_many :through
- Self-Referential Many-To-Many relationships where the relationship itself has data.