# Add column to find

**URL:** <https://www.ruby-forum.com/t/add-column-to-find/78278>\
**Category:** Rails\
**Created:** [December 4, 2006, 7:34pm UTC](https://www.ruby-forum.com/t/add-column-to-find/78278 "2006-12-04T19:34:16Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 4, 2006, 7:34pm UTC](https://www.ruby-forum.com/t/add-column-to-find/78278/1 "2006-12-04T19:34:16Z")

</div>

I have a find with a has\_many relationship using :include for the  
relationship. My question is how do i add an additional column to the  
query  
and keep all the associations in tact? I need to add a distance column  
in  
the output for some geocoding but can not figure out how to do this  
without  
being forced to find\_by\_sql.

Any ideas would be greatly appreciated.

Thanks  
Adam

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 4, 2006, 9:25pm UTC](https://www.ruby-forum.com/t/add-column-to-find/78278/2 "2006-12-04T21:25:03Z")

</div>

I’ll assume that the “column” in question isn’t in any table but being  
computed on the fly. If so, simply modify the ‘:select =\>’ parameter to  
add your additional column. I.e.

Model.find(:all, :select =\> ‘\*, insert\_ugly\_trig\_formula\_here AS  
distance’, :conditions =\> etc. etc.)

Notice the ‘\*,’ so that you still get the other columns you (and  
ActiveRecord) normally expect. Once the query returns you’ll have an  
@object.distance method you can call that gives you the computed  
distance for that record.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 4, 2006, 9:57pm UTC](https://www.ruby-forum.com/t/add-column-to-find/78278/3 "2006-12-04T21:57:10Z")

</div>

Thanks Jon.

Does this work as well with eager loading ? Thats why I included that  
i  
was using :include as well in my find. Just want to make sure that is  
not  
going to throw this in a hissy, because i have been trying :select with  
eager loading and not having any luck.

Thanks  
Adam

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 4, 2006, 10:39pm UTC](https://www.ruby-forum.com/t/add-column-to-find/78278/4 "2006-12-04T22:39:22Z")

</div>

Actually, no. Eager loading with :include =\> overwrites anything you put  
in the :select =\> parameter, so you can’t do both. Sorry, I didn’t pick  
up on the importance of the eager loading in your original question.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 4, 2006, 11:03pm UTC](https://www.ruby-forum.com/t/add-column-to-find/78278/5 "2006-12-04T23:03:26Z")

</div>

Thats what I thought. Any suggestions on a workaround? The only other  
thing I was thinking was somehow using a postgresql function or view to  
make  
an additional column available at the postgres level.

Thanks again for the help.  
Adam

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 5, 2006, 12:45am UTC](https://www.ruby-forum.com/t/add-column-to-find/78278/6 "2006-12-05T00:45:39Z")

</div>

The one time I’ve tried to do this, I didn’t actually need to eager  
load, I just needed to include other tables in the SELECT function and  
the WHERE conditions, so I used :joins =\> instead of :include =\> and  
that worked well for me. That may not work for you though. Your other  
options are, A) not eager load, or B) compute ‘distance’ for each item  
later on in a separate method.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 6, 2006, 6:09pm UTC](https://www.ruby-forum.com/t/add-column-to-find/78278/7 "2006-12-06T18:09:12Z")

</div>

Yes, :select and :joins play very nice together.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 6, 2006, 3:49am UTC](https://www.ruby-forum.com/t/add-column-to-find/78278/8 "2006-12-06T03:49:39Z")

</div>

Jon,

So does that mean i can use :select with :joins , but not :include ?

The function idea is a good recommendation, but I unfortunately need to  
sort the sql query by distance .

Thanks  
Adam
