Displaying only titles that starts with a defined caracter


#1

Hi,

I would like to know how to display only titles of products that starts
with a defined letter (ie: titles that begin with A)

Here is the method that I use to place all titles in alphabetical order:

class Livre < ActiveRecord::Base
validates_presence_of :titre;

def self.alpha
    find(   :all,
            :order  => "titre ASC"
            )
end

end

Any ideas ?

Thank you!


#2

What about

def self.alpha( letter = nil )
find :all, :order => “titre ASC”, :conditions => “titre LIKE
‘#{letter}%’”
end

That is assuming your using mysql


#3

On 3/23/06, weirdmonkey removed_email_address@domain.invalid wrote:

def self.alpha
    find(   :all,
            :order  => "titre ASC"
            )
end

end

Any ideas ?

Thank you!

How about?

class Livre < ActiveRecord::Base
def self.beginning_with(prefix)
clause = prefix + ‘%’
find(:all, :conditions => [“titre like ?”, clause], :order => “titre
ASC”)
end
end

Livre.beginning_with(‘A’)