Search code examples
ruby-on-railsrubyruby-on-rails-3ruby-on-rails-4activerecord

How to add "and !=" in ActiveRecord query


I need to add one more condition with AND and != in this query (i.e.: and properties.property_type_id != 'NULL')

How to add in this active record?

@properties = Property.where(category_id: Admin
  .select("category_id").where(id: current_user.id))

Solution

  • Try this

    @properties = Property.where("category_id IN (?) AND property_type_id IS NOT NULL", Admin.where(id: current_user.id).map(&:category_id) )
    

    I hope this will help