Ruby on Rails的通过自我参照以下/追随者关系HAS_MANY [英] Ruby on Rails has_many through self-referential following/follower relationships

查看:139
本文介绍了Ruby on Rails的通过自我参照以下/追随者关系HAS_MANY的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一些职位和线程上的has_many的:通过,但我还没有发现任何涉及具体是什么我想要做的。

There are a number of posts and threads on has_many :through, but I haven't found any that cover specifically what I'm trying to do.

我有一个用户模式和友谊的模式。用户拥有众多用户所关注他们,以及众多的追随者。它是通常的微模型

I have a User model and a Friendships model. A user has many users that are following them, as well as many followers. It is the usual Twitter model.

对于一个给定的用户,我想这回所后的用户和用户的追随者用户实际安装Active Record的关系。

For a given user, I want to setup Active Record relationships that return the actual users that are following the user and that the user is a follower of.

这些都是我所建立的关系:

These are the relationships that I have setup:

class User < ActiveRecord::Base

  has_many :following, :class_name => 'User', :through => :friendships, :foreign_key => 'user_id'

  has_many :followers, :class_name => 'User', :through => :friendships, :foreign_key => 'friend_id'

end

class Friendship < ActiveRecord::Base

  belongs_to :user
  belongs_to :following, :class_name => 'User', :foreign_key => 'friend_id'
  belongs_to :follower, :class_name => 'User', :foreign_key => 'user_id'

end

以下关系式的作品 - 它会生成以下连接:

The Following relationship works - it generates the below join:

SELECT `users`.* FROM `users` INNER JOIN `friendships` ON `users`.id = `friendships`.friend_id WHERE ((`friendships`.user_id = 1))

所有宏伟。

然而,从动关系不起作用。我已经尝试了许多变化,但多数似乎回到同一组的结果如下。

However, the Follower relationship does not work. I've tried a number of variations, but most seem to return the same set of results as Following.

我需要连接来进行设置,如下所示返回正确的结果集。

I need the join to be setup as follows to return the correct result set.

SELECT `users`.* FROM `users` INNER JOIN `friendships` ON `users`.id = `friendships`.user_id WHERE ((`friendships`.friend_id = 1)); 

我在哪里去了?

Where am I going wrong?

我可以使用上的has_many的finder_sql选项设置它,但它似乎应该有一个更好的办法。

I can set this up using the finder_sql option on the has_many, but it seems like there should be a better way.

has_many :followers, :class_name => 'User', :finder_sql => 'SELECT `users`.* FROM `users` INNER JOIN `friendships` ON `users`.id = `friendships`.user_id WHERE ((`friendships`.friend_id = #{ id }))'

谢谢!

我做了一点点进步,终于拿到了关系打破的关系分为两部分的工作,因为在这种反应结果显示:<一href="http://stackoverflow.com/questions/3114830/self-referential-has-many-through-with-customized-primary-key-issue">Self-referential的has_many:通过定制:主键问题

I made a bit of progress and finally got the relationship working by breaking the relationships into two parts, as was shown in this response: Self-referential has_many :through with customized :primary key issue

# FOLLOWING
has_many :friendships_to, :foreign_key => 'user_id', :class_name => 'Friendship'
has_many :following, :through => :friendships_to, :class_name => 'User'

# FOLLOWERS
has_many :friendships_from, :foreign_key => 'friend_id', :class_name => 'Friendship'
has_many :followers, :through => :friendships_from, :class_name => 'User'

然而,虽然是可能的有关系的的一行版本为以下

However, while it was possible to have a one-line version of the relationship for following

has_many :following, :class_name => 'User', :through => :friendships, :foreign_key => 'user_id'

我仍然无法得到它的工作的追随者。不过不知道怎么可以这样做?

I still wasn't able to get it to work for followers. Still wondering how this could be done?

推荐答案

您需要确保的ActiveRecord知道什么对用户#的朋友,同样的追随者源协会和指定类和foreign_key的关系是ActiveRecord的可以'牛逼外推该协会的名称。

You need to make sure ActiveRecord knows what the source association for the User#friends and likewise the followers and specify the class and foreign_key for the relationships that ActiveRecord can't extrapolate from the association names.

class Following < ActiveRecord::Base

  belongs_to :user
  belongs_to :followed, :class_name => 'User'

end

class User < ActiveRecord::Base

  has_many :followings
  has_many :friends, :through => :followings, :source => 'followed'

  has_many :followeds, :class_name => 'Following', :foreign_key => 'followed_id'
  has_many :followers, :through => :followeds, :source => :user

end

这篇关于Ruby on Rails的通过自我参照以下/追随者关系HAS_MANY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆