2
給定Car
有許多features
它們通過listed_features
連接。我如何才能找到所有cars
其中features
與ids
1
和2
和3
? cars
可以有更多的features
,但他們必須有前三個features
。其中has_many通過
的設置:
rails g model Car name:string
rails g model Feature name:string
rails g model ListedFeature car:references feature:references
應用程序/模型/ car.rb
class Car < ActiveRecord::Base
has_many :listed_features
has_many :features, through: :listed_features
end
應用程序/模型/ listed_features.rb
class ListedFeature < ActiveRecord::Base
belongs_to :car
belongs_to :feature
end
應用程序/模型/ featur es.rb
class Feature < ActiveRecord::Base
end
這是否保證車具有功能1和2和3? 'id:id_list'基本上會做1或2或3,計數只能確保有三個特徵。功能3,4,5的汽車是否與此查詢匹配? – Mischa
這是功能表中具有這些ID和哪些連接到汽車錶行的行數的計數。因此,如果汽車至少具備所需的功能,汽車將被退回。任何其他功能的存在將被忽略。 –
啊,是的,我現在看到了。謝謝。 – Mischa