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 = 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.
For more options, visit https://groups.google.com/d/optout.