来自rails3的activerecord不会返回连接表中的多个列

时间:2012-01-18 03:40:22

标签: mysql ruby-on-rails activerecord join ruby-on-rails-3.1

我有这个查询,当在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

1 个答案:

答案 0 :(得分:1)

使用.includes为我解决了这个问题。