Ignacio Palma Balboa Ignacio Palma Balboa - 1 year ago 118
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)

Thanks a lot!

Answer Source

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)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download