2011-02-06 52 views
4

我使用查找時方法像這樣得到這個錯誤不明確的列/ Rails的發現方法

Mysql::Error: Column 'id' in field list is ambiguous

self.prompts.find(:all, :select => 'id')

的模型被稱爲使用的has_many:通過聯想,所以MySQL抱怨有多個'id'列,因爲所有使用的3個表都有'id'列。

我查了一下,明白SQL結束時出了什麼問題,但不知道如何在ActiveRecord查找方法中解決它,而且我對我的SQL能力沒有把握,試圖滾動我自己的SQL查詢。有沒有一種方法可以將發現方法按摩成一種會發揮得很好的東西?

編輯

下面是相關演員型號代碼:

class Actor < ActiveRecord::Base 
    has_many :acts, :dependent => :destroy 
    has_many :decisions, :through => :acts, :order => 'created_at' 
    has_many :prompts, :through => :decisions, :order => 'id' 

回答

7

你需要更加明確要選擇哪個ID。例如:

 
self.prompts.find(:all, :select => 'prompts.id') #prompts is the table name 
+1

嗯,好了,擺脫了「曖昧」的錯誤,但現在它在抱怨`未知列在「decisions.actor_id',其中clause'`大概是因爲有兩個嵌套的has_many:通過它正在採取行動的協會。 – John 2011-02-06 06:51:49