[英]Rails HABTM association
我有自我參考的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_labels”,改為“ a_labels”。
class Label < AR::B
has_and_belongs_to_many :a_labels, ...
...
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.