0
我有類似這樣的兩種模式:ActiveRecord的:查詢記錄,其協會都要求性能相結合時,但單協會不具備的所有屬性
class Thing < ActiveRecord::Base
has_many :properties
end
class Property < ActiveRecord::Base
belongs_to :thing
end
# == Schema Information
#
# Table name: properties
#
# id :integer not null, primary key
# thing_id :integer not null
# property_1 :boolean
# property_2 :boolean
我需要一種方法來發現有任何所有things
屬性字段的組合,而不考慮屬性記錄(只要它們全部具有)。例如,我需要將property_1和property_2都設置爲true的所有things
,但它不必位於同一個Property記錄中。
thing = Thing.create
Property.create thing: thing, property_1: true
Property.create thing: thing, property_2: true
# I want to achieve something like this but with less queries
# (also my actual models are of course more complex
# so something like this is not practical nor efficient)
thing_ids = Property.where(property_1: true).pluck(:thing_id) &
Property.where(property_2: true).pluck(:thing_id)
這是比較簡單的通過處理的ActiveRecord產生的部分結果做的紅寶石,但我想,讓DB做的那麼多繁重越好。只有使用主動記錄才能以較笨拙的方式實現這一點?