如何优雅地将两个 Rails 查询链接在一起?
我有以下模型对象:
#<Insight id: 155, endorse_id: 15, supporter_id: 15, created_at: "2011-09-22 02:27:50", updated_at: "2011-09-22 02:27:50">
其中 Endorse has_many 见解,Supporter 有很多见解。
我可以通过以下方式查询数据库:
<块引用>Endorse.find(15).insights.count
Insight.find_all_by_supporter_id(15).count
如何优雅地将这两个查询链接在一起,以便搜索由 Endorse 15 和 Supporter 15 创建的所有见解?
I have the following model object:
#<Insight id: 155, endorse_id: 15, supporter_id: 15, created_at: "2011-09-22 02:27:50", updated_at: "2011-09-22 02:27:50">
Where Endorse has_many insights and Supporter has many insights.
I can query the db in the following way:
Endorse.find(15).insights.count
Insight.find_all_by_supporter_id(15).count
How can I elegantly chain both of these queries together where I can search for all Insights created by Endorse 15, and Supporter 15?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试这个
对于 Rails 3.0.x 和可能 3.1
对于 Rails 2.3.x
Try this
For Rails 3.0.x and probably 3.1
For Rails 2.3.x
我不确定您是否想要将它们链接在一起,因为每个查询都是不同的。如果他们之间存在关系,那么这对我来说就有意义了。例如,如果认可包含包含支持者的见解。
I'm not sure you would want to chain them together since each one is a different query. If there were relationships between them, then it would make sense to me. For example, if Endorse contained Insights which contained Supporters.