# How to build index from model

**URL:** <https://www.ruby-forum.com/t/how-to-build-index-from-model/155610>\
**Category:** Rails\
**Created:** [December 14, 2008, 5:04pm UTC](https://www.ruby-forum.com/t/how-to-build-index-from-model/155610 "2008-12-14T17:04:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![astropanic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/958977/32.png) [@astropanic](https://www.ruby-forum.com/u/astropanic)\
**Post date:** [December 14, 2008, 5:04pm UTC](https://www.ruby-forum.com/t/how-to-build-index-from-model/155610/1 "2008-12-14T17:04:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![astropanic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/958977/32.png) [@astropanic](https://www.ruby-forum.com/u/astropanic)\
**Post date:** [December 14, 2008, 7:46pm UTC](https://www.ruby-forum.com/t/how-to-build-index-from-model/155610/2 "2008-12-14T19:46:52Z")

</div>

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!

Jeremy  
[http://jeronrails.blogspot.com](http://jeronrails.blogspot.com)

---

<div class="post-metadata">

**Author:** ![astropanic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/958977/32.png) [@astropanic](https://www.ruby-forum.com/u/astropanic)\
**Post date:** [December 15, 2008, 11:17am UTC](https://www.ruby-forum.com/t/how-to-build-index-from-model/155610/3 "2008-12-15T11:17:28Z")

</div>

Thanks Man,  
It works nice 🙂

Regards

On Dec 14, 7:46 pm, Jeremy Weiskotten \<rails-mailing-l…@andreas-
