Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you conditionally build an ActiveRecord Query in Rails?

I'm trying to build a search results page in my rails app (still a noob) and I can't figure out how to build a query the rails way.

For example, if no parameters are present I want to return all results. If the user passes 1 to n optional parameters in the search form I want to add them to the query.

Next if they have a sort specified "price desc" or "year_built desc" or even a combination of both.

Finally use will_paginate to separate the results

# default to all listings
@listings = Mls.all

@listings.where("listing_price > ?", params[:listing_price]) unless params[:listing_price].blank?
# ... bunch of other search options ...
@listings.where("property_type = ?", params[:property_type]) unless params[:property_type].blank?

# order
@listings.order("some order by param") if some sort param
@listings.order("some order by param") if some other sort param

# paginate results
@listings.paginate(:page => params[:page])

Is there a "Rails" way of doing this?

like image 232
chrisan Avatar asked Jan 05 '13 20:01

chrisan


People also ask

What is ActiveRecord :: Base in Rails?

ActiveRecord::Base indicates that the ActiveRecord class or module has a static inner class called Base that you're extending.

What is ActiveRecord?

Active Record allows you to validate the state of a model before it gets written into the database. There are several methods that you can use to check your models and validate that an attribute value is not empty, is unique and not already in the database, follows a specific format, and many more.


2 Answers

Have you seen the (revised) Railscasts episode on advanced searches? Here is the link: http://railscasts.com/episodes/111-advanced-search-form-revised

The basic idea is to create a Search resource that will process the search params sent in through the form and in the background searches over the model in question (in your case Mls)

That way, instead of checking in the controller for the presence of certain parameters (e.g. params[:listing_price]) you could handle the conditions in your Search model (models/search.rb):

def find_listings
  listings = Mls.order(:name)
  listings = listings.where("listing_price > ?", listing_price) if listing_price.present?
  # ... more condition checking
  listings
end
like image 123
Andrea Singh Avatar answered Oct 22 '22 03:10

Andrea Singh


A couple of links:

Using third-party gems (metasearch)

Code from scratch

like image 38
dimuch Avatar answered Oct 22 '22 03:10

dimuch