# "Selecting" a calculated row / Using :select in the paginate

**URL:** <https://www.ruby-forum.com/t/selecting-a-calculated-row-using-select-in-the-paginate/51703>\
**Category:** Rails\
**Created:** [January 6, 2006, 2:25am UTC](https://www.ruby-forum.com/t/selecting-a-calculated-row-using-select-in-the-paginate/51703 "2006-01-06T02:25:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![IvSSSSn\_Vega\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7ab992/32.png) [@IvSSSSn\_Vega\_R](https://www.ruby-forum.com/u/IvSSSSn_Vega_R)\
**Post date:** [January 6, 2006, 2:25am UTC](https://www.ruby-forum.com/t/selecting-a-calculated-row-using-select-in-the-paginate/51703/1 "2006-01-06T02:25:10Z")

</div>

Hi,

I was trying to use the :select parameter to select a calculated row,  
but it appears that the parameter is not picked up by the paginate  
method.

I want to add the following to the select statement:

timediff(now(), created\_on) as age

So I tried:

@story\_pages, @stories = paginate :stories, {:per\_page =\> 10, :include  
=\> ‘user’, :select =\> ‘timediff(now(), story.created\_on) as story\_age’}

… to no avail.

I know I can create a method named “age” for the story model that can  
retrieve that calculation, but that’s a query for each element, so it’s  
very undesirable.

In case you’re wondering why I don’t calculate the “age” inside my Rails  
app, check this blog:

[http://railsexpress.de/blog/articles/2005/11/09/accelerate-your-dates](http://railsexpress.de/blog/articles/2005/11/09/accelerate-your-dates)

If you know of a solution or alternative, I’ll greatly appreciate your  
help.

Sincerely,  
Ivan V.

---

<div class="post-metadata">

**Author:** ![IvSSSSn\_Vega\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7ab992/32.png) [@IvSSSSn\_Vega\_R](https://www.ruby-forum.com/u/IvSSSSn_Vega_R)\
**Post date:** [January 6, 2006, 3:03am UTC](https://www.ruby-forum.com/t/selecting-a-calculated-row-using-select-in-the-paginate/51703/2 "2006-01-06T03:03:23Z")

</div>

IvÃ¡n Vega R. wrote:

> @story\_pages, @stories = paginate :stories, {:per\_page =\> 10, :include  
> =\> ‘user’, :select =\> ‘timediff(now(), story.created\_on) as story\_age’}
> 
> … to no avail.

I’m somewhat sure that paginate (and find for that matter) don’t know  
what to do with the :select option.

Try using find\_by\_sql()

---

<div class="post-metadata">

**Author:** ![IvSSSSn\_Vega\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7ab992/32.png) [@IvSSSSn\_Vega\_R](https://www.ruby-forum.com/u/IvSSSSn_Vega_R)\
**Post date:** [January 6, 2006, 3:29pm UTC](https://www.ruby-forum.com/t/selecting-a-calculated-row-using-select-in-the-paginate/51703/3 "2006-01-06T15:29:57Z")

</div>

Check out this code snippet:

> **[BigBold - Informasi Tentang Bisnis dan Marketing](http://www.bigbold.com)**
>
> BigBold ialah situs yang memberikan segala macam Informasi Tentang Bisnis dan Marketing

Even better is the updated version in ‘toolmantim’s’ post at the bottom.

---

<div class="post-metadata">

**Author:** ![IvSSSSn\_Vega\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7ab992/32.png) [@IvSSSSn\_Vega\_R](https://www.ruby-forum.com/u/IvSSSSn_Vega_R)\
**Post date:** [January 6, 2006, 9:03pm UTC](https://www.ruby-forum.com/t/selecting-a-calculated-row-using-select-in-the-paginate/51703/4 "2006-01-06T21:03:59Z")

</div>

Check  
[http://railsexpress.de/blog/articles/2005/11/06/the-case-for-piggy-backed-attributes](http://railsexpress.de/blog/articles/2005/11/06/the-case-for-piggy-backed-attributes)  
😉

---

<div class="post-metadata">

**Author:** ![IvSSSSn\_Vega\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7ab992/32.png) [@IvSSSSn\_Vega\_R](https://www.ruby-forum.com/u/IvSSSSn_Vega_R)\
**Post date:** [January 6, 2006, 9:00pm UTC](https://www.ruby-forum.com/t/selecting-a-calculated-row-using-select-in-the-paginate/51703/5 "2006-01-06T21:00:58Z")

</div>

Thanks!
