xyious
xyious

Reputation: 1073

comparing attributes (in controller)

This seems like it is the simplest thing ever, but it cost me two hours of searching and trying stuff, so please give me a quick answer.

I am basically following railstutorial for an application, just that I have a field privileges (integer) instead of admin (boolean).

Now I'm trying to implement an admin? function that returns (privileges > 0).

I tried the following (and many other versions) in app/models/user.rb:

  # is user admin ?
  def admin?
    0 < self.privileges
  end

Obviously that doesn't work. Also tried putting it in app/controllers/users_controller.rb. Please help.

Upvotes: 0

Views: 49

Answers (1)

BroiSatse
BroiSatse

Reputation: 44685

def admin?
  !!privileges && 0 < privileges
end

or

def admin?
  0 < privileges.to_i
end

Another options would be to set a default 0 to the column.

Upvotes: 1

Related Questions