Hi, I have a application where I have a book model. I have a book controller, it has an action named "index_titles" What should I write in the action of this controller to get a list with the distinct first letters for each book model in the view ? For example when I have five books: "Amazing title" "Bookworm is comming" "Basic for beginners" "Denial of service" "UPS development guide" I will have four links in the view with the anchors "A", "B", "D" and "U" respectively. I''m a experienced PHP developer, but I''m a newcommer to Ruby. What is the optimal rails way to accomplish this ? Regards --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
astropanic wrote:> Hi, > > I have a application where I have a book model. > I have a book controller, it has an action named "index_titles" > > What should I write in the action of this controller to get a list > with the distinct first letters for each book model in the view ? > > For example when I have five books: > > "Amazing title" > "Bookworm is comming" > "Basic for beginners" > "Denial of service" > "UPS development guide" > > I will have four links in the view with the anchors "A", "B", "D" and > "U" respectively. > > I''m a experienced PHP developer, but I''m a newcommer to Ruby. What is > the optimal rails way to accomplish this ? > > RegardsSomething like this in your controller (assuming you''re on Rails 2.1 or later): def index_titles @first_letters = Book.all(:select => ''title'', :order => ''title'').map { |book| book.title.first } end This code fetches the title of every book (in alpha order) and extracts just the first letter of each title. Be sure to have an index on the title column. Then iterate on @first_letters in your view to render a link to each letter. Hope that helps! Jeremy http://jeronrails.blogspot.com -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks Man, It works nice :) Regards On Dec 14, 7:46 pm, Jeremy Weiskotten <rails-mailing-l...@andreas- s.net> wrote:> astropanic wrote: > > Hi, > > > I have a application where I have a book model. > > I have a book controller, it has an action named "index_titles" > > > What should I write in the action of this controller to get a list > > with the distinct first letters for each book model in the view ? > > > For example when I have five books: > > > "Amazing title" > > "Bookworm is comming" > > "Basic for beginners" > > "Denial of service" > > "UPS development guide" > > > I will have four links in the view with the anchors "A", "B", "D" and > > "U" respectively. > > > I''m a experienced PHP developer, but I''m a newcommer to Ruby. What is > > the optimal rails way to accomplish this ? > > > Regards > > Something like this in your controller (assuming you''re on Rails 2.1 or > later): > > def index_titles > @first_letters = Book.all(:select => ''title'', :order => ''title'').map { > |book| book.title.first } > end > > This code fetches the title of every book (in alpha order) and extracts > just the first letter of each title. Be sure to have an index on the > title column. > > Then iterate on @first_letters in your view to render a link to each > letter. > > Hope that helps! > > Jeremyhttp://jeronrails.blogspot.com > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---