Help correct and improve this WHERE

Hi,

I am trying to build a search form with multiple field input. And while
the single field search is working just fine, I am having some issues
creating the WHERE with the two fields joined. Here is the code.

def index
@cities = City.order(“province_id, name”).page(params[:page])

# Need to create this condition for now I have added a place holder.
if (!params[:name_like].blank?) && (!params[:province_like].blank?)
  @cities = City.order("province_id, name").page(params[:page])

# This if works just fine
elsif !params[:name_like].blank?
  @cities = City.where(:name.matches => ('%' + params[:name_like] +

‘%’)).order(“province_id, name”).page(params[:page])

# This if works just fine
elsif !params[:province_like].blank?
  @cities = City.joins(:province).where(:province => {:name.matches

=> (’%’ + params[:province_like] + ‘%’)}).order(“province_id,
name”).page(params[:page])

end

end

Also is there a better way to write the same piece of code?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs