2014-09-26 3 views
1

레일 애플리케이션의 루비에서 다음을 달성하려고합니다. 이것은 또한 제 데이터베이스 레이아웃과 모델 이름입니다. IllustationRuby 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 %> 

Debug

내가 어떻게이 일을 아무 생각이 없다 다른 방법은 나는이 일을 잘하고 있는지 잘 모르겠다. 어떤 도움을 주셔서 감사합니다.

+1

"is_followed_by"관계를 원하십니까? –

+1

이것 좀보세요 : http://stackoverflow.com/questions/3397920/relationship-like-twitter-followers-followed-in-activerecord 또는 this one http://stackoverflow.com/questions/6762919/ruby-on -rails-has-many-through-self-referential-follower-follower-relationships – MrYoshiji

+0

그냥 찾아 보았습니다. 내가 찾고있는 것이 있습니다. – Mike

답변

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 

이제, 당신은 아마 주로 사용합니다 두 협회가 사용자에 대해 "추종자"와 "users_i_follow을".

mdchaney$ bundle exec rails c 
Loading development environment (Rails 4.1.5) 
2.1.1 :001 > u = User.create(:email => '[email protected]', :name => 'Leader Guy') 
=> #<User id: 1, email: "[email protected]", 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 => '[email protected]', :name => 'Follower Guy') 
=> #<User id: 2, email: "[email protected]", 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 => '[email protected]', :name => 'Follower Guy, III') 
=> #<User id: 3, email: "[email protected]", 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: "[email protected]", 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: "[email protected]", name: "Follower Guy", created_at: "2014-09-26 18:49:13", updated_at: "2014-09-26 18:49:13">, #<User id: 3, email: "[email protected]", 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: "[email protected]", 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: "[email protected]", name: "Leader Guy", created_at: "2014-09-26 18:48:52", updated_at: "2014-09-26 18:48:52">]>