[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Active-Record Sql: selecting only few columns

Rajat Garg

3/12/2008 12:28:00 PM

[Note: parts of this message were removed to make it a legal post.]

Hi Guys,

I am writing a sql query -
User.find(:all, :order=> 'u.created_on DESC', :limit =>2,
:joins => "as u left join photos p on u.id = p.user_id")

But i want this query to return only 3 parameters (name, photo_url, id)
instead of all. Is there any ActiveRecord Way of selecting only those
fields.

I will really appreciate your help.

Thanks

--
Rajat Garg


Ph: 206-499-9495
Add: 1314 Spring Street, #412
Seattle, WA 98104
Web: http://www.piloto...
-----------------------------------------------------------------------------------------------------
Flying is the second greatest thrill known to man. Landing is the first!

2 Answers

David Palm

3/12/2008 12:57:00 PM

0

:select should be your friend here.

On Wed, 12 Mar 2008 21:27:51 +0900, Rajat Garg wrote:
> Hi Guys,
>
> I am writing a sql query -
> User.find(:all, :order=> 'u.created_on DESC', :limit =>2,
> :joins => "as u left join photos p on u.id = p.user_id")
>
> But i want this query to return only 3 parameters (name, photo_url, id)
> instead of all. Is there any ActiveRecord Way of selecting only those
> fields.
>
> I will really appreciate your help.
>
> Thanks

Rob Biedenharn

3/12/2008 12:57:00 PM

0

On Mar 12, 2008, at 8:27 AM, Rajat Garg wrote:
> Hi Guys,
>
> I am writing a sql query -
> User.find(:all, :order=> 'u.created_on DESC', :limit =>2,
> :joins => "as u left join photos p on u.id = p.user_id")
>
> But i want this query to return only 3 parameters (name, photo_url,
> id)
> instead of all. Is there any ActiveRecord Way of selecting only those
> fields.
>
> I will really appreciate your help.
>
> Thanks
>
> --
> Rajat Garg
>
> Ph: 206-499-9495
> Add: 1314 Spring Street, #412
> Seattle, WA 98104
> Web: http://www.piloto...
> -----------------------------------------------------------------------------------------------------
> Flying is the second greatest thrill known to man. Landing is the
> first!


Add a :select => 'name, photo_url, id' to your User.find, but be aware
that it means the User objects will be readonly since they don't have
enough information to construct the SQL update command later.

-Rob

Rob Biedenharn http://agileconsult...
Rob@AgileConsultingLLC.com