[英]Rails5 How can I build a feed with ActiveRecord Relation objects
I'd like to build a feed with Microposts: 我想用Microposts建立一个feed:
First I collect all Microposts from my following with visible_for 'following'
首先,我用visible_for 'following'
从我的关注中收集所有Micropost
following_ids_subselect = "SELECT followed_id FROM relationships
WHERE follower_id = :user_id"
following_feed = Micropost.where("visible_for = 'following'
AND user_id IN (#{following_ids_subselect})", user_id: id)
Then I collect all Microposts from my following (AND status "contact") with visible_for 'contact'
然后,我用visible_for 'contact'
从我的关注对象(AND状态为“ contact”)收集所有Microposts
contacts_ids_subselect = "SELECT followed_id FROM relationships
WHERE follower_id = :user_id
AND status = 'contact'"
contacts_feed = Micropost.where("visible_for = 'contacts'
AND user_id IN (#{contacts_ids_subselect})", user_id: id)
Is there any way to get following_feed
and contacts_feed
together? 有什么办法可以使following_feed
和contacts_feed
在一起? that I can write: 我可以写:
@feed_items = global_feed.paginate(page: params[:page])
In other words: global_feed
should contain all Microposts from users the current user follows with visible_for 'followers'
换句话说: global_feed
应该包含来自当前用户的所有Microposts,当前用户使用visible_for 'followers'
+ all Microposts from users the curent user follows with status 'contact'
with visible_for 'contacts'
+当前用户的所有来自用户的微visible_for 'contacts'
, status 'contact'
, visible_for 'contacts'
Update: 更新:
Relationship 关系
class Relationship < ApplicationRecord
# Table name: relationships
#
# id :integer(4) not null, primary key
# follower_id :integer(4)
# followed_id :integer(4)
# status :string
belongs_to :follower, class_name: "User"
belongs_to :followed, class_name: "User"
end
User 用户
class User < ApplicationRecord
has_many :active_relationships, class_name: "Relationship",
foreign_key: "follower_id",
dependent: :destroy
has_many :following, through: :active_relationships, source: :followed
has_many :passive_relationships, class_name: "Relationship",
foreign_key: "followed_id",
dependent: :destroy
has_many :followers, through: :passive_relationships, source: :follower
(..)
end
Micropost 微信
class Micropost < ApplicationRecord
#
# Table name: microposts
#
# id :integer(4) not null, primary key
# user_id :integer(4)
# content :string
# visible_for :string
belongs_to :user
default_scope -> { order('created_at DESC') }
end
#paginate
is a method only available on ActiveRecord relations. #paginate
是仅在ActiveRecord关系上可用的方法。 Which means you will need to select the data you need in one query. 这意味着您将需要在一个查询中选择所需的数据。
So if I'm reading your sample correctly, you want all Micropost
s from users the current user follows that are visible for contacts
or following
. 因此,如果我正确地阅读了您的示例,则希望所有来自当前用户的用户的Micropost
都可以在contacts
或following
中看到。
You can select them in one query like so 您可以像这样在一个查询中选择它们
following_user_ids = user.following.pluck(:id)
contact_user_ids = Relationship.where(status: "contact", follower_id: user.id).pluck(:follower_id)
Microposts.where("(visible_for = 'followers' and user_id in (?)) OR (visible_for = "contacts" and user_id in (?)"), following_user_ids, contact_user_ids))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.