Ruby on Rails Sunday, July 5, 2015

Before I respond, would you please verify the fields in your table User, specfically roleid.

On Sunday, July 5, 2015 at 6:50:03 AM UTC-4, Padmahas Bn 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.

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 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/e6c598e2-5477-42ad-97f2-bbc0a5f77778%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment