0

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

我哪里弄错了?谢谢。

4

1 回答 1

1

看起来您只需要在您的关联名称中添加一个“s”。而不是“a_label”,改为“a_labels”。

class Label < AR::B
  has_and_belongs_to_many :a_labels, ...
    ...
end
于 2012-07-14T17:49:03.580 回答