weirdmonkey
2006-Mar-23 05:09 UTC
[Rails] Displaying only titles that starts with a defined caracter.
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! -- Posted via http://www.ruby-forum.com/.
Liquid
2006-Mar-23 05:25 UTC
[Rails] Displaying only titles that starts with a defined caracter.
What about def self.alpha( letter = nil ) find :all, :order => "titre ASC", :conditions => "titre LIKE ''#{letter}%''" end That is assuming your using mysql On 3/23/06, weirdmonkey <xfile7@hotmail.com> wrote:> > 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! > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060323/1b99764a/attachment.html
Wilson Bilkovich
2006-Mar-23 05:26 UTC
[Rails] Displaying only titles that starts with a defined caracter.
On 3/23/06, weirdmonkey <xfile7@hotmail.com> wrote:> 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! >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'')