1
2間HABTM關係的十字路口,我有如下關係:確定軌道3
class Article < ActiveRecord::Base
has_and_belongs_to_many :required_certs, :class_name=>"Certification"
...
end
class User < AR::B
has_and_belongs_to_many :certifications
...
end
我希望能夠找到具有相同認證作爲文章的所有用戶。因此,如果一篇文章有證書A和B,並且用戶有A,B,C,D,則該用戶將被退回。只有A或B的用戶不會。因此,在本質上,像
User.joins(:certifications).where('certifications.id IN (?)', @article.required_certs.collect{|c| c.id})
而不是使用IN操作符,雖然,我想我不得不使用因爲我只想要誰擁有所有指定證書的用戶。
我很難過,所以任何幫助將不勝感激!
感謝
什麼是由此產生的SQL? – orj 2011-04-25 15:31:24
我想我有一個類似的問題:http://stackoverflow.com/questions/5779354/rails3-activerecord-fetching-all-records-that-have-a-and-b-through-a-has-many。不幸的是你的解決方案對我來說不合理。 User.attributes_for_sql從何而來? – orj 2011-04-25 15:34:19