[英]Multiple columns from join tables are not returned by activerecord in rails3
我有这个查询,当在mysql cli中运行时会给出正确的结果,但在由rails执行时却不能
查询:
SELECT t1.id, t1.event_id, t2.content, t2.created_at, t3.title FROM t1
INNER JOIN t2 ON t2.id = t1.event_id
INNER JOIN t3 ON t3.id = t2.id
WHERE (t1.id in (SELECT id FROM t4 WHERE attr = 20))
ORDER BY t1.created_at DESC
LIMIT 15
我用了
t1.find_by_sql "<above_sql_query>"
但它仅返回与t1有关的列。 结果是一个数组:
[#<t1 id: 3, event_id: 3>]
我也尝试过
t1.find(:all, :select => "<select attributes as above>", :joins => "as above", :conditions => "as above", :limit => 15, :order => "t1.created_at DESC")
但仍然给出相同的结果,只返回与t1有关的属性。 请帮助我找到执行该命令的最佳方法。
谢谢,pR
.includes
的使用为我解决了这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.