相关疑难解决方法(0)

ActiveRecord查询联盟

我用Ruby on Rail的查询接口写了几个复杂的查询(至少对我来说):

watched_news_posts = Post.joins(:news => :watched).where(:watched => {:user_id => id})
watched_topic_posts = Post.joins(:post_topic_relationships => {:topic => :watched}).where(:watched => {:user_id => id})
Run Code Online (Sandbox Code Playgroud)

这两个查询都可以自行完成.两者都返回Post对象.我想将这些帖子合并到一个ActiveRelation中.由于某些时候可能有数十万个帖子,因此需要在数据库级别完成.如果是MySQL查询,我可以简单地使用UNION运算符.有没有人知道我是否可以用RoR的查询界面做类似的事情?

union activerecord ruby-on-rails active-relation

82
推荐指数
7
解决办法
6万
查看次数