ruby on rails - Accessing an instance method in an active record query -
given following 2 models:
class wheel belongs_to :car def self.flat where(flat: true) end
and
class car has_many :wheels def flats self.wheels.flat end def has_flats? flats.count > 0 end
i need query cars flat tires. i'm wondering why isn't working in cars model?:
def self.with_flats where(:has_flats?) end
or
def self.with_flats where(:has_flats? == true) end
this isn't returning correct records. ideas?
define scope in car model:
class car has_many :wheels scope :having_flat_wheels, joins(:wheels).where("wheels.flat=?", true).uniq ...... end
then cars flat tires:
car.having_flat_wheels
Comments
Post a Comment