Active record without JOIN
I have
class Autosalon < ActiveRecord::Base
has_many :autos
end
c开发者_StackOverflow社区lass Auto < ActiveRecord::Base
belongs_to :autosalon
end
autosalon has flag active=1 and date of registration, auto has flag active=1
How get all autos with active flag in autosalons with active flag without JOIN?
Without any joins/includes, you could use a SQL in
query:
Auto.where(:active => 1).where("autosalon_id in (select id from autosalons where active=1)")
Something like this should do it:
active_autosalons = Autosalon.where(:active => 1)
active_autos = Autos.where(:autosalon_id => active_autosalons.map(&:id)).where(:active => 1)
Get the list of 'active' Autosalons first, then filter Autos by the Autosalon ids and the active column.
精彩评论