Ruby on rails 创建Rails关联
我很难理解如何创建以下关联:Ruby on rails 创建Rails关联,ruby-on-rails,ruby-on-rails-3,activerecord,relationships,model-associations,Ruby On Rails,Ruby On Rails 3,Activerecord,Relationships,Model Associations,我很难理解如何创建以下关联: 用户有许多相册 相册里有很多照片 用户可以关注其他人的特定相册(并间接地关注相册的所有者),并在“新闻提要”类型的场景中显示照片,因此他们之间必须建立某种关系 以下是我的联想: class User < ActiveRecord::Base has_many :photo_albums has_many :photos has_many :relationships, :foreign_key => "follower_id",
- 用户有许多相册
- 相册里有很多照片
- 用户可以关注其他人的特定相册(并间接地关注相册的所有者),并在“新闻提要”类型的场景中显示照片,因此他们之间必须建立某种关系
class User < ActiveRecord::Base
has_many :photo_albums
has_many :photos
has_many :relationships, :foreign_key => "follower_id",
:dependent => :destroy
has_many :reverse_relationships, :foreign_key => "followed_id",
:class_name => "Relationship",
:dependent => :destroy
has_many :followings, :through => :relationships, :source => :followed
has_many :followers, :through => :reverse_relationships, :source => :follower
end
class PhotoAlbum < ActiveRecord::Base
belongs_to :user
has_many :photos
end
class Photo < ActiveRecord::Base
belongs_to :user
belongs_to :photo_album
end
class Relationship < ActiveRecord::Base
belongs_to :follower, :foreign_key => "follower_id", :class_name => "User"
belongs_to :followed, :foreign_key => "followed_id", :class_name => "User"
end
class用户“跟随者id”,
:dependent=>:destroy
有很多:反向关系,:外键=>“跟在\u id后面”,
:class_name=>“关系”,
:dependent=>:destroy
有很多:following,:through=>:relationships,:source=>:following
拥有多个:追随者,:通过=>:反向关系,:源=>:追随者
结束
类PhotoAlbum“跟随者id”,:类名=>“用户”
属于以下项::外部项=>“后续项id”,:类名称=>“用户”
结束
我怎样才能从用户正在关注的相册中获取所有照片
示例:
- 简有两本相册,“简的公共物品”和“简的私人物品” 每一张都有照片
- Bob只关注“Jane的公开资料”,所以我怎么才能退回照片 通过ActiveRecord关联仅从该相册中删除
bob.following.photos
的东西,它只返回bob正在跟踪的相册中的照片。。。或者甚至bob.following.photo_相册
来获取bob正在关注的所有相册的集合
我知道要做到这一点可能还有很长的路要走,但是有没有更简单的方法来使用ActiveRecord关联呢
感谢您提供的任何建议或指导!我真的很感激 您可能想看看
amistad
gem at,它为您提供了好友和友谊功能。在你的情况下,朋友就是追随者
然后,您可以执行类似于
user.friends.each do | f |
的操作来检索user
的所有追随者,然后执行f.photos
来获取每个追随者的所有照片。您可能需要查看amistad
gem at,它为您提供了朋友和友谊功能。在你的情况下,朋友就是追随者
然后,您可以执行类似于
user.friends.each do | f |
的操作来检索user
的所有追随者,然后执行f.photos
来获取每个追随者的所有照片。在用户模型中尝试以下操作:
def followings_photos
Photo.where(:user_id => self.followings.collect(&:id))
end
在用户模型中尝试以下操作:
def followings_photos
Photo.where(:user_id => self.followings.collect(&:id))
end
按照您的设置方式,Follow将一个用户与另一个用户关联,而不是用户与相册关联,这就是您所描述的。听起来好像你想要
class Relationship < ActiveRecord::Base
belongs_to :follower, :foreign_key => "follower_id", :class_name => "User"
belongs_to :followed, :foreign_key => "photo_album_id", :class_name => "PhotoAlbum"
end
按照您的设置方式,Follow将一个用户与另一个用户关联,而不是用户与相册关联,这就是您所描述的。听起来好像你想要
class Relationship < ActiveRecord::Base
belongs_to :follower, :foreign_key => "follower_id", :class_name => "User"
belongs_to :followed, :foreign_key => "photo_album_id", :class_name => "PhotoAlbum"
end
谢谢David,但这会收集他们正在关注的所有人的所有照片,即使他们没有关注照片所在的相册。我希望它只返回他们正在关注的相册中的照片。我在原始问题中添加了一个示例,以更好地解释它。Hanks David,但它收集了他们正在关注的所有人的所有照片,即使他们没有关注照片所在的相册。我希望它只返回他们正在关注的相册中的照片。我在原始问题中添加了一个示例,以更好地解释它感谢答案,但我想看看我是否可以在执行长路径的do循环或复杂SQL之前使用AR关联来完成这一任务。谢谢你的回答,但是我想看看我是否可以在执行长时间的do循环或复杂的SQLAhhh之前,使用AR关联来完成这项工作,我已经非常接近了!非常感谢。现在我可以做类似于
bob.followers.groupby(&:user\u id)的事情。数一数他也在跟踪多少人。啊,我太接近了!非常感谢。现在我可以做类似于bob.followings.groupby(&:user\u id)。计算以获得他也在跟踪多少人。