Forum: Ruby on Rails Displaying only titles that starts with a defined caracter.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
weirdmonkey (Guest)
on 2006-03-23 07:09
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!
Daniel -. (Guest)
on 2006-03-23 07:25
(Received via mailing list)
What about

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

That is assuming your using mysql
Wilson B. (Guest)
on 2006-03-23 07:27
(Received via mailing list)
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')
This topic is locked and can not be replied to.