简体   繁体   English

rail3中的activerecord不会返回联接表中的多列

[英]Multiple columns from join tables are not returned by activerecord in rails3

I have this query which when run in mysql cli gives the proper result but not when executed by rails 我有这个查询,当在mysql cli中运行时会给出正确的结果,但在由rails执行时却不能

Query: 查询:

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

I used 我用了

t1.find_by_sql "<above_sql_query>"

but it only returns those columns that concern t1. 但它仅返回与t1有关的列。 The result is an array: 结果是一个数组:

[#<t1 id: 3, event_id: 3>] 

I also tried with 我也尝试过

t1.find(:all, :select => "<select attributes as above>", :joins => "as above", :conditions => "as above", :limit => 15, :order => "t1.created_at DESC")

but still gives the same result, only returns attributes concerning t1. 但仍然给出相同的结果,只返回与t1有关的属性。 Please help me in finding the best way to execute that command. 请帮助我找到执行该命令的最佳方法。

Thanks, pR 谢谢,pR

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM