Temporarily I'm using <td><%= rolename[0] %> </td>  to eliminate brackets
and double quotes.
But there should be better way to do this. Its an obvious situation where
we have to select specific fields of a record which satisfies some
condition.
All I want is Active Record query equivalent to this
SELECT role_name FROM role WHERE role.id = user.roleid

Thank you

On Sun, Jul 5, 2015 at 4:20 PM, Padmahas Bn <[email protected]> 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
> <http://guides.rubyonrails.org/active_record_querying.html#selecting-specific-fields>.
> But the thing is this will produce the SQL *Select field_name FROM Model*.
> I want to extend this to include *where* clause.
>
> Why I want to do this
>
> This is the code in my index page
>
> <tbody>
> <% @users.each do |user| %>
> <% *rolename = Role.where(id: user.roleid).pluck(:role_name)* %>
> <tr>
> <td><%= user.userid %></td>
> <td><%= user.fname %></td>
> <td><%= user.lname %></td>
> <td><%= user.email %></td>
> <td><%= user.phno %></td>
> <td><%= rolename %></td>
> <td><%= link_to 'Show', user ,:class => 'btn btn-default' %></td>
> <td><%= link_to 'Edit', edit_user_path(user),:class => 'btn btn-default'
> %></td>
> <td><%= link_to 'Delete', user, method: :delete, data: { confirm: 'Are you
> sure?' } ,:style => 'color:#FFFFFF', :class => 'btn btn-danger'%></td>
> </tr>
> <% end %>
> </tbody>
>
> The large font code after loop is giving the result what I want but the
> result is enclosed with big brackets and double quotes like this ["
> result"]. Because that code will return an array of values.
>
> 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.
> Again I tried with this *<% rolename = Role.find_by_sql("SELECT role_name
> FROM ROLES, USERS WHERE roles.id <http://roles.id> = users.roleid.to_i")
> %>, *its giving error *PG::UndefinedTable: ERROR: missing FROM-clause
> entry for table "roleid"*
> By comparing all types of queries I thought it is good to include where
> clause along with Model.select. But couldn't find anywhere.
>
> Please help me
>
> Thank you
>
> --
> 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 [email protected].
> To post to this group, send email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rubyonrails-talk/5d915337-b273-49b5-a93d-1583679be8fc%40googlegroups.com
> <https://groups.google.com/d/msgid/rubyonrails-talk/5d915337-b273-49b5-a93d-1583679be8fc%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/CAC_h78KBL92H31pq537onA%2B5DfOtWWugMiefNnSws7x_fNXkQg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to