# How to build index from model

**URL:** <https://discuss.rubyonrails.org/t/how-to-build-index-from-model/37343>\
**Category:** rubyonrails-talk\
**Created:** [December 14, 2008, 3:20pm UTC](https://discuss.rubyonrails.org/t/how-to-build-index-from-model/37343 "2008-12-14T15:20:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![astropanic](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@astropanic](https://discuss.rubyonrails.org/u/astropanic)\
**Post date:** [December 14, 2008, 3:20pm UTC](https://discuss.rubyonrails.org/t/how-to-build-index-from-model/37343/1 "2008-12-14T15:20:38Z")

</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:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [December 14, 2008, 6:46pm UTC](https://discuss.rubyonrails.org/t/how-to-build-index-from-model/37343/2 "2008-12-14T18: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 &nbsp;&nbsp;@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://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@astropanic](https://discuss.rubyonrails.org/u/astropanic)\
**Post date:** [December 15, 2008, 10:16am UTC](https://discuss.rubyonrails.org/t/how-to-build-index-from-model/37343/3 "2008-12-15T10:16:36Z")

</div>

Thanks Man, It works nice 🙂

Regards
