2014-09-27 96 views
0
Option.includes(:yaoption).references(:yaoption).select('options.id, yaoptions.name').where(options: {dept_id: 100}) 

結果在下面的SQL查詢:ActiveRecord的「提及」關鍵字忽略列「選擇」部分

SELECT 
    options.id AS t0_r0, 
    options.title AS t0_r1, 
    options.dept_id AS t0_r2, 
    options.mode AS t0_r3, 
    options.content AS t0_r4, 
    options.yaoption_id AS t0_r5, 
    options.created_at AS t0_r6, 
    options.updated_at AS t0_r7, 
    yaoptions.id AS t1_r0, 
    yaoptions.name AS t1_r1, 
    options.id, 
    yaoptions.name 
FROM 
    options 
    LEFT OUTER JOIN yaoptions ON yaoptions.id = options.yaoption_id 
WHERE 
    options.dept_id = 100 
ORDER BY 
    options.id ASC 

ActiveRecord的查詢與references關鍵詞似乎忽視了select部分。

我想看到的只是:

SELECT 
    options.id, 
    yaoptions.name 
FROM 
    options 
    INNER JOIN yaoptions ON yaoptions.id = options.yaoption_id 
WHERE 
    options.dept_id = 100 

當我使用joins正在發送正確的查詢:

Option.joins(:yaoption).select('options.id, yaoptions.name').where(options: {dept_id: 100}) 

但結果包含options表只id S:

#<ActiveRecord::Relation [#<Option id: 1>, #<Option id: 2>, #<Option id: 3>]> 

我想要一個帶有指定字段的查詢。

回答

0

雖然inspect列出Option只有idname屬性也是可訪問的。