Ruby on Rails Sunday, July 5, 2015

On 5 July 2015 at 11:50, Padmahas Bn <padmahas@gmail.com> wrote:
> I am trying to retrieve only one field instead of all field. Hence
> guides.rubyonrails.org suggest to use Model.select("field_name,
> separated_by, comma") here. But the thing is this will produce the SQL
> Select field_name FROM Model. I want to extend this to include where clause.
> ...
> So I found I have to use Model.select to retrieve single object. Then I
> tried <% rolename = Role.select("role_name").where(id: user.roleid) %> but
> its returning some invalid value like 00x30000658487.

Try
Role.where(id: user.roleid).select("role_name")

Or even better
user.role.select("role_name")

Though I have to ask why you need to do the select? Why not just use
Role.where(id: user.roleid).role_name
or again even better
user.role.role_name

It is not likely that the slight saving in processor/memory will make
any difference in practice to the application performance. I always
recommend not complicating code by attempting to optimise until it
proves to be necessary. Almost always the bottlenecks in an
application will not be in the area you expect.

Colin

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLsbrwmJiTJohT448QVJraeu2sMkxydSDfL8kxzr201Rpg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment