kchak
kchak

Reputation: 8062

In Rails 5, how to render the output of a query?

I am trying to render the output of a query in my views. I am running Rails verion 5.4.2.1

@users = User.with_attached_avatar.where("id not in (:list)", list: xyz)

The view code is

<ul>
<%= @users %>
</ul>

The view code will work, for example, when the controller sends @users = @team.members (assuming member is a user) and there is a _user.html.erb partial defined. So there is no problem when I use this technique to render a list of association objects.

While the query itself is successful (I checked), the view only prints out the string representation of the returned relations object #<User::ActiveRecord_Relation:0x00007f9468025478>, not the list.

I can see that the query returns an ActiveRecord_Relation object but an association like @user.messages would return a ActiveRecord_Associations_CollectionProxy.

How do I render the array as a list, like you can do with associations, and partials?

Upvotes: 0

Views: 147

Answers (1)

John
John

Reputation: 1973

This should work

<%= render partial: "user", collection: @users, as: :user %>
//_user.html.erb
<li>User info: <%= user.name %></li>

Upvotes: 3

Related Questions