0

我有自己的參考habtm協議。Rails HABTM協會

class Label < AR::B 
    has_and_belongs_to_many :a_label, :class_name => "Label", 
      :join_table => "a_labels", 
      :foreign_key => "label_id", 
      :association_foreign_key => "a_label_id", 
      :uniq => true 
end 

但是,當我創建查詢(用squeel):

Label.select{:title}.where do 
     id.in(Label.select{:a_label_id}.joins(:a_labels).where{ 
      labels.title.in(list) 
      }) 

模式:

labels: 
id | title | description | created_at 

a_labels 
label_id | a_label_id  

我得到了錯誤:

ActiveRecord::ConfigurationError: 
    Association named 'a_labels' was not found; perhaps you misspelled it? 

哪裏錯了? 謝謝。

回答

1

看起來你只需要添加一個「s」到你的關聯名稱。而不是「a_label」,改爲「a_labels」。

class Label < AR::B 
    has_and_belongs_to_many :a_labels, ... 
    ... 
end 
+0

我有一個更復雜的查詢。我更新了問題。 – Mike 2012-07-14 18:38:24