jbearden
jbearden

Reputation: 1869

Rails 3: Class method authentication

I have a simple authorization function but I am having a rough time getting implemented correctly. Each post is issued a :key_code when it is created and the user (not sessioned) can type the code and have their post deleted. The problem is, is that regardless of what they type for the key_code the object gets passed.

Why is this always returning 0?

    field :key_code, type: Integer

    def self.auth(id, key_code)
        post = Post.where(:id => id).first

        puts key_code        #for testing:
        puts post.key_code   #for testing: 

        if post.key_code == key_code
          return 1
        else
          return 0
        end

end

Console test:

1.9.3p194 :001 > a = Post.auth('5032f3254ff9fcf10100001b','42745590875')
42745590875
42745590875
=> 0 

Upvotes: 0

Views: 41

Answers (2)

jbearden
jbearden

Reputation: 1869

I got it. It was also passing the second number through as a string! Doh

Upvotes: 0

Brandan
Brandan

Reputation: 14983

Looks like you're using an assignment operator (=) instead of equality (==). Change that line to:

if post.key_code == key_code

Upvotes: 2

Related Questions