簡體   English   中英

rail3中的activerecord不會返回聯接表中的多列

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM