简体   繁体   中英

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

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. 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. Please help me in finding the best way to execute that command.

Thanks, pR

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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