Forum: Ruby on Rails Adding objects to a :through association

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.
B13a9240291ffa22215214316bd365de?d=identicon&s=25 Hendrik Mans (Guest)
on 2006-03-31 20:43
(Received via mailing list)
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
9f0f89bbd9e1ecfbaab6584e429b7a2f?d=identicon&s=25 Josh Susser (jsusser)
on 2006-03-31 20:56
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
B13a9240291ffa22215214316bd365de?d=identicon&s=25 Hendrik Mans (Guest)
on 2006-03-31 21:08
(Received via mailing list)
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
27c170f482104299af279902be0a9c26?d=identicon&s=25 Trevor Squires (Guest)
on 2006-03-31 21:41
(Received via mailing list)
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
5db037676defee3a41c1ed9ac2fb15c3?d=identicon&s=25 Chris Larson (Guest)
on 2006-03-31 21:53
(Received via mailing list)
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
C70d253e33d13e1de4bb82fd06fffdb3?d=identicon&s=25 Wolfgang Borgon (Guest)
on 2006-04-06 04:19
(Received via mailing list)
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
90842970ceaf7ed19136ce7b6f8e1455?d=identicon&s=25 Michael Daines (Guest)
on 2006-04-06 06:55
(Received via mailing list)
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
This topic is locked and can not be replied to.