Note: Read this post to learn how to implement the same method using the find_all function. - Rory, July 3, 2005
In the lyrics website I’m developing, I’d like to be able to use URLs like this in my administrative back-end:
Have you found this post helpful?
If so, I'd appreciate if you could indicate so by pressing the Google Plus button below.
http://www.website.com/admin/artists/list/A
where, when I visit this URL, a list of artists who’s name begins with the letter A is displayed. This requires the use of the Ruby on Rails find_by_sql method, as there is no way of querying for artists in this manner using the built-in Active Record.
The first thing I did was add a new method to my Artist model that will allow me to search for artists by the first letter of their name.
artist.rb
class Artist < ActiveRecord::Base
has_many :songs, :order => "song_title", :dependent => true
has_many :albums, :order => "album_name", :dependent => true
def self.find_by_first_letter(letter = "A")
find_by_sql ["select * from artists where ucase(left(artist_name, 1)) = ?", letter]
end
end
As you can see, the method find_by_first_letter takes in an argument called “letter” and uses this argument in the function call to find_by_sql. You’ll notice that a question mark appears in the SQL statement where we would actually like the value associated with “letter” to go. Queries created this way are called Parameterized SQL Queries, and by using them, we avoid the possibility of having someone tamper with our query using SQL injection.
Then, in our action, all we have to do is call our new method.
admin_controller.rb
@artists = Artist.find_by_first_letter(@params["letter"])
Related posts:
4 Comments to “Ruby on Rails find_by_sql Example”
Post comment
Recommended Services
Recent Posts
- Fantastic new corporate themes for WordPress
- Vistaprint offers FREE t-shirts, too!
- 80+ “Your Ad Here” 125 x 125 banners
- 5 Minute Long Tail SEO Drill: More Traffic, Better SERPs
- iPhone and iPod Touch app statistics: OS adoption, purchasing rates
Recent Comments
- Neerav on Using JavaScript to validate one or more grouped checkboxes
- Jacob on Using JavaScript to validate one or more grouped checkboxes
- kaify on Using JavaScript to validate one or more grouped checkboxes
- JC Goldenstein on How To: Cloak your Affiliate Links for Free in Under 3 Seconds
- Bail Bonds Los Angeles on Amazon Web Services on Rails
Categories
- .net
- acoustic guitar
- affiliate marketing
- ajax
- amazon associates
- blogging
- books
- business ideas
- c#
- code igniter
- dealdotcom
- google adsense
- google adwords
- internet marketing
- iPhone
- javascript
- leadership
- make money online
- mortgage goal
- msn adcenter
- networking
- personal development
- php
- ppc
- ruby
- ruby on rails
- seo
- text-link-ads
- web development
- yahoo search marketing





Rory on Rails » Ruby on Rails find_all Example says:
[...] Recent Posts
Ruby on Rails find_all ExampleWow! WordPress + My Posts + FireFox == BAD.Ruby on Rails find_by_sql ExampleGet Free [...]
rick says:
find :all, :conditions => ['ucase(left(artist_name, 1)) = ?', letter]
Mike says:
Or
find :all, :conditions => [upper(artist_name) like ?', letter + "%"]
which may or may not be faster, depending on the indexes you have set up. a like query like this should be able to use a standard index on artist_name, but if you have an index on ucase(left(artist_name)), then Rick’s solution would be faster, probably.
New Ruby says:
My following code not working, please guide, thanks.
@params['body'].each do |contact|
@c = Contact.find(:all, :conditions=> ["contact_id = ?", contact["contact_id"]])
if @c.count > 0
# update record
else
# insert record
end
end