ruby-on-rails - "Append"两个不同的ActiveRecord::Relation 变成一个ActiveRecord::Relation

标签 ruby-on-rails ruby activerecord

我的处境与ICTylor's post here 类似。 .

所以我有:

user1=User.find(1);
user2=User.find(2);
written=Micropost.where("user_id=2");
written.class #=> ActiveRecord::Relation
written.length   #=> 50
replied=Micropost.where("response = 2 ")  #=> 1
replied.class #=> ActiveRecord::Relation

现在如果我:

alltogether= written + replied;
alltogether.class #=> Array
alltogether.length #=> 51

但是我想要一些相当于做的事情:

all_sql= Micropost.where("user_id = 2  OR response = 2")
all_sql.class #=> ActiveRecord::Relation
all_sql.length #=> 51

换句话说我想以某种方式追加一个Micropost.where(...)找到的记录由另一个 Micropost.where(...) 转换成一个 ActiveRecord::Relation 对象。产生等同于 all_sql 但分两步到达。

一些解释。应用程序的这一部分旨在为回复消息提供类似 twitter 的功能。

例如:当User.id = 1的用户向User.id=2发送此消息时:

@2:嘿,这是回复。

应用程序将使用以下参数创建一个微博:

@post= Micropost.create(user:1, content:"@2: 嘿这是回复", response: 2)

因此,response 仅表示回复的 receiver id。如果消息不是回复类型,则 response = nil

按照这个想法,我希望能够:

def replies_to(user)
  Micropost.where("response = #{user.id}")
end

def written_posts_by(user)
  Micropost.where("user_id = #{user.id}")
end

def interesting_posts(user)
  replies= replies_to(user)
  written= written_posts_by(user)
  #And now the question arises!

  new_relation= replies union written#<---How can I do this!?
end

最佳答案

这个设计本身就有些问题,如果一个帖子回复了两个或多个接收者怎么办?表中也会有太多的空数据,这是不好的。

无论如何,基于当前只允许一个接收器的设计,模型需要进行一些更改。

class User < ActiveRecord::Base
  has_many :posts
  has_many :replies, class_name: 'Post', foreign_key: 'response_id'

  def written_posts
    posts
  end

  def posts_replied_to_me
    replies
  end
end

上述变化说明:

  1. API 在 User 表中更好,因此它们不需要像 (user) 这样的参数
  2. 有了关联,以上两个公共(public)方法实际上是不必要的,仅供当前使用。您可以直接从协会 API 访问这些帖子。

现在是interesting_posts。由于上述重构,您不再依赖上述方法来构建聚合查询,因为它们具有不同的结构。正如 Mori 提到的那样,打补丁是一种解决方案,但我本人更愿意尽可能不要触及库。

我更喜欢一种方法来聚合专用于这种情况的查询。

def interested_posts
  Post.where(interesting_criteria)
end

private
def interesting_criteria
  conditions = []
  conditions << written_by_me
  conditions << replied_to_me
  conditions << foo_bar
  conditions.join(' AND ')
end

def written_by_me
  "user_id = #{self.id}"
end

def replied_to_me
  "response_id = #{self.id}"
end

def foo_bar
  "feel free to add more"
end

关于ruby-on-rails - "Append"两个不同的ActiveRecord::Relation 变成一个ActiveRecord::Relation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19324605/

相关文章:

ruby-on-rails - Make 未被识别为内部或外部命令

ruby-on-rails - 如何在连接表中按条件接收对象

ruby-on-rails - 在活跃商户应用程序中使用两步验证的卡(信用卡/借记卡)会发生什么情况?

ruby-on-rails - 如何在一些数据之后进行迭代?

ruby - CouchRest - 检查文档 ID 是否存在

ruby-on-rails - 如何正确设置 Active Paypal Adaptive Payment IPN Notifications?

ruby-on-rails - 我怎样才能使ActiveRecord的调用也返回要在json中呈现的列名?

ruby-on-rails - 将参数传递给关注点,联合使用

ruby-on-rails - Rails 4 中 rspec 功能测试的重构代码出现问题

ruby-on-rails - Mavericks ruby​​ on rails SQLite3 问题