Having trouble with boolean method

I'm checking if a user is an admin, and then I will show a delete button
if he is. For whatever reason, it's automatically setting the boolean to
true. It says is_admin: false in the console when I pull up the user's
record.

Check status of user
def is_admin?
  self.username
end

view method
<% if @user.is_admin? %>
<%= link to 'Delete', remove_bla_bla_path(user), method: :destroy %>
<% end %>

Have any ideas to why the method is still showing the button?

I'm checking if a user is an admin, and then I will show a delete button
if he is. For whatever reason, it's automatically setting the boolean to
true. It says is_admin: false in the console when I pull up the user's
record.

Check status of user
def is_admin?
self.username
end

This method is going to return the username, which is probably set to something, and that means true when you ask this way. You probably meant to check if the admin attribute on that user model.

Walter

Walter Davis wrote in post #1181250:

self.username
end

This method is going to return the username, which is probably set to
something, and that means true when you ask this way. You probably meant
to check if the admin attribute on that user model.

Walter

I have a is_admin column on my users table. Set to false by default. How
can I use it alongside the method? I will manually set true of course
for specified users.

I got it! had to set self.is_admin? Thanks for pointing me in the right
direction.

I got it! had to set self.is_admin? Thanks for pointing me in the right
direction.

I'm not sure this is doing what you think it is. If you have a boolean column on your model called is_admin, then Rails will define an is_admin? method for you (it actually does this for all columns, not just the booleans). When you redefine your is_admin? method to self.is_admin?, you are moving it up to become a class method, which means that when you call @user.is_admin? you are getting the Rails-added version of the method. If you called User.is_admin? you would get an error, or nothing, because the method as you wrote it here doesn't make a lot of sense. (As an instance method, it will always return true, assuming that all of your users have a non-empty username. As a class method, assuming you called it, I think you would get an error saying that self.username is undefined.)

Walter