Complex query not filtering as expected

Below please see my non-working query This returns all records where I wanted it to return only records with an item_type_id matching the instance variable of the same name, hasn’t already been sold, and the title is “like” params[:search] In mysql like syntax is “%text%”, and finally ordered by title Can someone correct my code for me please @items=Item.includes(:sales).where(“unit_price is not null”).where(:sales => { :item_id => nil } ).where(“item_type_id=? and title like ?”, @item_type_id, “%#{params[:search]}%”).order(:title)