# How Select just some columns?

**URL:** <https://www.ruby-forum.com/t/how-select-just-some-columns/164917>\
**Category:** Rails\
**Created:** [April 7, 2009, 11:54am UTC](https://www.ruby-forum.com/t/how-select-just-some-columns/164917 "2009-04-07T11:54:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![spitfire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a5b964/32.png) [@spitfire](https://www.ruby-forum.com/u/spitfire)\
**Post date:** [April 7, 2009, 11:54am UTC](https://www.ruby-forum.com/t/how-select-just-some-columns/164917/1 "2009-04-07T11:54:59Z")

</div>

Hi,

if I do:  
user = User.find :all  
render :xml =\> user.to\_xml

I obviously get the user records whith ALL the columns (name, surname,  
tel, email, …)

How I can get just the column email (for example) ?

ps.

I wan’t to use User.find\_by\_sql(‘Select mail from users’)

thanks

---

<div class="post-metadata">

**Author:** ![spitfire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a5b964/32.png) [@spitfire](https://www.ruby-forum.com/u/spitfire)\
**Post date:** [April 7, 2009, 12:18pm UTC](https://www.ruby-forum.com/t/how-select-just-some-columns/164917/2 "2009-04-07T12:18:06Z")

</div>

On Apr 7, 10:54 am, Ale Ds [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,
> 
> if I do:  
> user = User.find :all  
> render :xml =\> user.to\_xml
> 
> I obviously get the user records whith ALL the columns (name, surname,  
> tel, email, …)

depending on what you’re doing, either use the :select option on the  
call to find or the :only/:except options on to\_xml may be what you  
want.

Fred

---

<div class="post-metadata">

**Author:** ![spitfire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a5b964/32.png) [@spitfire](https://www.ruby-forum.com/u/spitfire)\
**Post date:** [April 7, 2009, 12:33pm UTC](https://www.ruby-forum.com/t/how-select-just-some-columns/164917/3 "2009-04-07T12:33:14Z")

</div>

‘:select’ is perfect:

users = User.find(:all, :select = ‘email’)

I overlooked it in RoR api.  
Thank you !

Alessandro

Frederick C. wrote:

> On Apr 7, 10:54ï¿½am, Ale Ds [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Hi,
> > 
> > if I do:  
> > user = User.find :all  
> > render :xml =\> user.to\_xml
> > 
> > I obviously get the user records whith ALL the columns (name, surname,  
> > tel, email, …)
> 
> depending on what you’re doing, either use the :select option on the  
> call to find or the :only/:except options on to\_xml may be what you  
> want.
> 
> Fred
