简体   繁体   English

Rails5如何使用ActiveRecord Relation对象构建提要

[英]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_feedcontacts_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都可以在contactsfollowing中看到。

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.

相关问题 如何通过铁轨交叉两个ActiveRecord关系对象? - How can i intersection two ActiveRecord Relation objects by rails? 如何在Rails5上进行事件下载? - How can I do event on downloads on Rails5? 如何在 Rails 中插入一堆 ActiveRecord 对象和关系? - How can I upsert a bunch of ActiveRecord objects and relationships in Rails? 从Rails API文档中,我如何知道ActiveRecord :: Relation支持“ collect()”? - From the Rails API docs, how can I know that ActiveRecord::Relation supports “collect()”? 如何在不更改数组顺序的情况下将数组转换为Rails ActiveRecord :: Relation? - How can i convert an array to an rails ActiveRecord::Relation without changing array order? Rails ActiveRecord-如何确定多态关系中的所有者? - Rails ActiveRecord - How do I determine the owner in a polymorphic relation? Rails ActiveRecord:链接ActiveRecord_Relation对象的方法吗? - Rails ActiveRecord: way to chain ActiveRecord_Relation objects? Rails5 /如何为导入的对象设置默认属性值 - Rails5 / how to set default attribute value for imported objects 如何在Rails中基于ActiveRecord_Relation获得ActiveRecord_Relation? - How to get ActiveRecord_Relation based on a ActiveRecord_Relation in rails? 如何构建避免在Rails中使用ActiveRecord :: HasManyThroughCantAssociateThroughHasOneOrManyReflection错误的对象? - How to build objects that avoid a ActiveRecord::HasManyThroughCantAssociateThroughHasOneOrManyReflection erros in rails?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM