Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 906103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:19:48+00:00 2026-05-15T16:19:48+00:00

In one particular Railcasts episode Ryan talks about advanced search and in that he

  • 0

In one particular Railcasts episode Ryan talks about advanced search and in that he uses some code so as to find the conditions for the search. As its working isn’t explained I wanted some clarification regarding it.

def products
 @products ||= find_products
end

private

def find_products
 Product.find(:all, :conditions => conditions)
end

def keyword_conditions
["products.name LIKE ?", "%#{keywords}%"] unless keywords.blank?
end

def minimum_price_conditions
 ["products.price >= ?", minimum_price] unless minimum_price.blank?
end

def maximum_price_conditions
 ["products.price <= ?", maximum_price] unless maximum_price.blank?
end

def category_conditions
 ["products.category_id = ?", category_id] unless category_id.blank?
end

def conditions
 [conditions_clauses.join(' AND '), *conditions_options]
end

def conditions_clauses
 conditions_parts.map { |condition| condition.first }
end

def conditions_options
 conditions_parts.map { |condition| condition[1..-1] }.flatten
end

def conditions_parts
 private_methods(false).grep(/_conditions$/).map { |m| send(m) }.compact
end

I would welcome any information as to how this works especially the method products as he even calls it as products.name etc.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T16:19:49+00:00Added an answer on May 15, 2026 at 4:19 pm

    He defines some methods for the conditions in his search form: keyword_conditions, minimum_price_conditions ans so on. products.name means the field name from the table products.

    The method

    def conditions_parts
      private_methods(false).grep(/_conditions$/).map { |m| send(m) }.compact
    end
    

    uses reflection to look at the private methods of this class which have the name that ends with _conditions (The regex /_conditions$/) and joins only those that don’t return null (compact)

    The method

    def conditions
      [conditions_clauses.join(' AND '), *conditions_options]
    end
    

    adds a AND keyword between the conditions and passes the result to Product.find which makes the SQL query and returns the result set.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to search a table to find all rows where one particular field
I have a GridView that (in one particular instance) would contain about 5000 rows,
I have an with my code and I have one particular textarea that I
I have one particular FLA that is crashing every time I try to compile
I have an effect that when you hover over this one particular area, it
One particular quirk of the (otherwise quite powerful) re module in Python is that
A common occurrence I have with one particular project is that it requires the
What MySQL query will do a text search and replace in one particular field
I want build a search service for one particular thing. The data is freely
As an example, one particular application state may have a home view that just

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.