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?

-- 
Posted via http://www.ruby-forum.com/.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to