Rails HABTM协会

问题描述:

我有自己的参考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? 

哪里错了? 谢谢。

看起来你只需要添加一个“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