Home>

Assuming that there is a USER model as shown below, I want to search for a person whose admin is false and display the name, but I don't know how to do it.

I did it like this, but the name is not displayed.

@name = User.where.not (admin: true)
<% = @name%>

User

name | admin
Yamada | true
Nishikawa | false
Morikawa | false

  • Answer # 1

    @name = User.where.not (admin: true)

    If it is a question requirement, it is easier to readwhere (admin: false)instead ofwhere.not (admin: true).
    Also, since the content that comes back is a collection of user information, the variable name@nameis inappropriate.
    Should be@users.
    If you want to displayname, you need to fetch@usersone by one and accessname.

    Based on the above,

    rb file

    @users = User.where (admin: false)

    erb file

    <% @ users.each do | user |%>
      <% = user.name%>
    <% end%>

    I think this is possible.

Related articles