我的处境与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
上述变化说明:
- API 在 User 表中更好,因此它们不需要像
(user)
这样的参数 - 有了关联,以上两个公共(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/