[Rails] Only one side of join table is being saved
In my app, User objects can follow each other, and be followed. The two
relationships are distinct.
I'm seeing that when I set `user_a.follows << user_b` that
`user_b.followed_by.count` still == 0. Why?
When I play in the console, I see:
[code]
$ jordan = User.new(:name=>"Jordan")
=> #<User id: nil, name: "Jordan">
$ matt = User.new(:name=>"Matt")
=> #<User id: nil, name: "Matt">
$ matt.followers << jordan
=> [#<User id: nil, name: "Jordan">]
$ matt.followers.first
=> #<User id: nil, name: "Jordan">
$ jordan.friends.first
=> nil
$ matt.save
SQL (14.1ms) INSERT INTO "users" ("name") VALUES (?) [["name",
"Matt"]]
SQL (0.3ms) INSERT INTO "users" ("name") VALUES (?) [["name",
"Jordan"]]
SQL (0.4ms) INSERT INTO "followings" ("followee_id", "follower_id")
VALUES (?, ?) [["followee_id", nil], ["follower_id", 2]]
=> true
[/code]
My objects are defined as:
[code]
class User < ActiveRecord::Base
has_many :follower_followee_rel,
:class_name => "Following",
:foreign_key => 'followee_id',
:dependent => :destroy
has_many :friends,
:through => :follower_followee_rel,
:source => :followee
has_many :followee_follower_rel,
:class_name => 'Following',
:foreign_key => 'follower_id',
:dependent => :destroy
has_many :followers,
:through => :followee_follower_rel,
:source => :follower
end
class Following < ActiveRecord::Base
belongs_to :followee,
:class_name => 'User'
belongs_to :follower,
:class_name => 'User'
end
[/code]
Totally ignoring the second half of the relationship.
No errors are raised. What's going on?
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home