Ignacio Palma Balboa Ignacio Palma Balboa - 5 months ago 22
SQL Question

Active record AND Operator

how do i turn this into one WHERE statement? I want to look for records where a property is an apartment and also is for sale.

def apartment
@apartments_for_sale = Property.where(for_sale: true)
@real_apartments = @apartments_for_sale.where(apartment: true)
end


Thanks a lot!

Answer

You should be able to do:

def apartment
    @apartments = Property.where(for_sale: true, apartment: true)

     #OR via method chaining:

    @apartments = Property.where(for_sale: true).where(apartment: true)
end
Comments