Having trouble with boolean method

24 views
Skip to first unread message

David Williams

unread,
Feb 7, 2016, 7:21:26 PM2/7/16
to rubyonra...@googlegroups.com
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?

--
Posted via http://www.ruby-forum.com/.

Walter Lee Davis

unread,
Feb 7, 2016, 8:23:09 PM2/7/16
to rubyonra...@googlegroups.com

> On Feb 7, 2016, at 7:20 PM, David Williams <li...@ruby-forum.com> wrote:
>
> 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

>
>
> 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?
>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> 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-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/e271d6cdea52f5ca85bb048cb9f21afd%40ruby-forum.com.
> For more options, visit https://groups.google.com/d/optout.

David Williams

unread,
Feb 7, 2016, 8:50:42 PM2/7/16
to rubyonra...@googlegroups.com
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.

David Williams

unread,
Feb 7, 2016, 8:53:13 PM2/7/16
to rubyonra...@googlegroups.com
I got it! had to set self.is_admin? Thanks for pointing me in the right
direction.

Walter Lee Davis

unread,
Feb 8, 2016, 7:53:46 PM2/8/16
to rubyonra...@googlegroups.com

> On Feb 7, 2016, at 8:52 PM, David Williams <li...@ruby-forum.com> wrote:
>
> 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

>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> 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-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/ea8144300fcde31b5e123e495c61a58e%40ruby-forum.com.
Reply all
Reply to author
Forward
0 new messages