INCLUDE_DATA

Article written

  • on 03.07.2005
  • at 09:19 PM
  • by Rory

Ruby on Rails find_all Example 2

Jul3

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

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.

subscribe to comments RSS

There are 2 comments for this post

  1. [...] n Rails

    Recent Posts
    Ruby on Rails find_all ExampleWow! WordPress [...]

  2. name says:

    named_scope would be better

Please, feel free to post your own comment

* these are required fields