【问题标题】:Ruby on Rails Bi-directional associationsRuby on Rails 双向关联
【发布时间】:2014-09-26 17:59:40
【问题描述】:

我正在尝试在我的 ruby​​ on rails 应用程序中实现以下目标。这也是我的数据库布局和模型名称。

但是,这对我来说并不奏效。

用户模型:

class User < ActiveRecord::Base
    has_many :follows
    has_many :users, :through => :follows
end

关注模型:

class Follow < ActiveRecord::Base
    belongs_to :user, :foreign_key => :follow_user_id
end

我用这个来调试:

<% current_user.follows.each do |x| %>
    <%= debug x.user %>
<% end %>

我不知道如何反过来做,我什至不确定我是否做对了。任何帮助表示赞赏。

【问题讨论】:

标签: ruby-on-rails


【解决方案1】:

你的想法是对的,只是你还没有完成关联。首先,请注意,用户两次连接到关注 - 每个方向一次。

rails g model User email:string name:string
rails g model Follow user:references follow_user:references

然后:

class User < ActiveRecord::Base
  has_many :follows, inverse_of: :user
  has_many :followers, through: :follows

  has_many :followings, class_name: 'Follow', foreign_key: :follow_user_id, inverse_of: :follower
  has_many :users_i_follow, through: :followings, source: :user
end

class Follow < ActiveRecord::Base
  belongs_to :user, inverse_of: :follows
  belongs_to :follower, inverse_of: :followers, class_name: 'User', foreign_key: :follow_user_id
end

现在,对于一个用户,您可能主要使用两个关联:“followers”和“users_i_follow”。

mdchaney$ bundle exec rails c
Loading development environment (Rails 4.1.5)
2.1.1 :001 > u = User.create(:email => 'test@comor.net', :name => 'Leader Guy')
 => #<User id: 1, email: "test@comor.net", name: "Leader Guy", created_at: "2014-09-26 18:48:52", updated_at: "2014-09-26 18:48:52"> 

2.1.1 :002 > u2 = User.create(:email => 'follower@comor.net', :name => 'Follower Guy')
 => #<User id: 2, email: "follower@comor.net", name: "Follower Guy", created_at: "2014-09-26 18:49:13", updated_at: "2014-09-26 18:49:13"> 

2.1.1 :003 > u3 = User.create(:email => 'follower3@comor.net', :name => 'Follower Guy, III')
 => #<User id: 3, email: "follower3@comor.net", name: "Follower Guy, III", created_at: "2014-09-26 18:49:25", updated_at: "2014-09-26 18:49:25"> 

2.1.1 :004 > u.followers
 => #<ActiveRecord::Associations::CollectionProxy []> 

2.1.1 :005 > Follow.create(:user => u, :follower => u2)
 => #<Follow id: 1, user_id: 1, follow_user_id: 2, created_at: "2014-09-26 18:50:12", updated_at: "2014-09-26 18:50:12"> 

2.1.1 :006 > Follow.create(:user => u, :follower => u3)
 => #<Follow id: 2, user_id: 1, follow_user_id: 3, created_at: "2014-09-26 18:50:14", updated_at: "2014-09-26 18:50:14"> 

2.1.1 :007 > u = User.find(1)
 => #<User id: 1, email: "test@comor.net", name: "Leader Guy", created_at: "2014-09-26 18:48:52", updated_at: "2014-09-26 18:48:52"> 

2.1.1 :008 > u.followers
 => #<ActiveRecord::Associations::CollectionProxy [#<User id: 2, email: "follower@comor.net", name: "Follower Guy", created_at: "2014-09-26 18:49:13", updated_at: "2014-09-26 18:49:13">, #<User id: 3, email: "follower3@comor.net", name: "Follower Guy, III", created_at: "2014-09-26 18:49:25", updated_at: "2014-09-26 18:49:25">]> 

2.1.1 :009 > u.follows
 => #<ActiveRecord::Associations::CollectionProxy [#<Follow id: 1, user_id: 1, follow_user_id: 2, created_at: "2014-09-26 18:50:12", updated_at: "2014-09-26 18:50:12">, #<Follow id: 2, user_id: 1, follow_user_id: 3, created_at: "2014-09-26 18:50:14", updated_at: "2014-09-26 18:50:14">]> 

2.1.1 :010 > u2 = User.find(2)
 => #<User id: 2, email: "follower@comor.net", name: "Follower Guy", created_at: "2014-09-26 18:49:13", updated_at: "2014-09-26 18:49:13"> 

2.1.1 :011 > u2.followers
 => #<ActiveRecord::Associations::CollectionProxy []> 

2.1.1 :012 > u2.follows
 => #<ActiveRecord::Associations::CollectionProxy []> 

2.1.1 :013 > u2.followings
 => #<ActiveRecord::Associations::CollectionProxy [#<Follow id: 1, user_id: 1, follow_user_id: 2, created_at: "2014-09-26 18:50:12", updated_at: "2014-09-26 18:50:12">]>

2.1.1 :014 > u2.users_i_follow
 => #<ActiveRecord::Associations::CollectionProxy [#<User id: 1, email: "test@comor.net", name: "Leader Guy", created_at: "2014-09-26 18:48:52", updated_at: "2014-09-26 18:48:52">]> 

【讨论】:

    【解决方案2】:

    这也可以通过单个has_many :through 关联来完成。见the related answer

    它是针对friends / friendships 问题的,但它与followers / follows 问题基本相同。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多