VN:F [1.9.11_1134]
Rating: 0.0/5 (0 votes cast)

In an earlier blog post, I listed an example of using the find_by_sql function to return a collection of Artist objects. I have since changed my search method to use the find_all function, instead.

artist.rb (old code)
def self.find_by_first_letter(letter = "A")
   find_by_sql ["select a.* from artists a where ucase(left(artist_name, 1)) = ?", letter]
end

NOTE FROM THE AUTHOR, RORY HANSEN
Have you found this post helpful?
If so, I'd appreciate if you could indicate so by pressing the Google Plus button below.

artist.rb (new code)
def self.find_by_first_letter(letter = "A")
   find_all ["ucase(left(artist_name, 1)) = ?", letter]
end

I think using the find_all function instead of the find_by_sql function follows the “Ruby on Rails way” more closely. Plus, it’s shorter and, perhaps, easier to understand for people who aren’t familiar with SQL.

VN:F [1.9.11_1134]
Rating: 0.0/5 (0 votes cast)

Related posts:

  1. Ruby on Rails find_by_sql Example
  2. Amazon Web Services on Rails
  3. Related Drop-down Lists with Ruby on Rails and AJAX
  4. Implementing Search in Ruby on Rails
  5. Models with Associations

2 Comments to “Ruby on Rails find_all Example”

Post comment