0

I have self reference habtm assotiation.

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

But when i create query (with squeel):

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

Schema:

 labels:
 id | title | description | created_at

 a_labels
 label_id | a_label_id    

I got error:

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

Where I was mistaken? Thanks.

Mike
  • 1,042
  • 1
  • 8
  • 14

1 Answers1

1

Looks like you just need to add an "s" to your association name. Instead of "a_label", change to "a_labels".

class Label < AR::B
  has_and_belongs_to_many :a_labels, ...
    ...
end
steakchaser
  • 5,198
  • 1
  • 26
  • 34