我有这个查询,当在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
答案 0 :(得分:1)
使用.includes
为我解决了这个问题。