只保存连接表的一面?

时间:2011-09-18 08:12:56

标签: ruby-on-rails-3 activerecord many-to-many

在我的应用中,用户对象可以互相跟随,并被跟踪。这两种关系是截然不同的。

当我设置user_a.follows << user_b user_b.followed_by.count仍然== 0时,我看到了这一点。

当我在控制台玩游戏时,我看到:

$ 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 

我的对象定义为:

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

完全无视关系的后半部分。

不会引发任何错误。发生了什么事?

2 个答案:

答案 0 :(得分:2)

加入关系不起作用,除非已加入的两个模型都已保存。您可以在SQL的第3行看到为followee_id插入nil,因为jordan还没有id。

在你检查约旦的朋友之前你还需要保存亚特,因为做matt.followers << jordan被视为亚光的修改,除非保存哑光,否则不会影响其他任何事情。

答案 1 :(得分:1)

实现Michael Fairley's suggestion得到ActiveRecord来建立关系,但关系仍被定义错误。

最终解决方案是在加入记录之前保存记录,并修复向后的外键。

当时:

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

现在:

has_many  :follower_followee_rel,  
          :class_name         => 'Following',
          :foreign_key        => 'follower_id',
          :dependent          => :destroy

has_many  :friends, 
          :through            => :follower_followee_rel, 
          :source             => :followee

has_many  :followee_follower_rel,           
          :class_name         => 'Following',
          :foreign_key        => 'followee_id',
          :dependent          => :destroy

has_many  :followers, 
          :through            => :followee_follower_rel, 
          :source             => :follower

发现变更@

:follower_followee_rel, :foreign_key=>'followee_id'

:followee_follower_rel, :foreign_key=>'followee_id'