I’m still in the learning phase of Ruby and RoR, but I’ve already encountered a problem.
I’ve created a scaffold called Users with fields “name” and “password”.
Later on, I also added more fields such as “pin” and “salt”.
Now, while User.find_by_name and find_by_password work perfectly fine, for some reason, find_by_pin doesn’t seem to work. I’ve tried googling a bit but found nothing.
I also tried the same method in the ruby console (rails console) and for some reason, it works perfectly fine there – it returns the correct data just as expected.
Any ideas?
EDIT 1:
As somebody stated in the comments, let’s say I’m using this code: (yes, cookies[:pin] is set)
def get_user
user = User.find_by_pin(cookies[:pin])
if user.pin == cookies[:pin]
'yes'
end
end
and later I do this:
<%= get_user %>
it returns this:
undefined method `pin' for nil:NilClass
EDIT 2:
I tried this:
def get_user
user = User.find_by_name('Crembo')
user.pin # returns 753e345a113471a6775e242f444704fe
user = User.find_by_pin(user.pin)
if user.nil?
'is nil'
else
'exists'
end
end
and it returns ‘is nil’. How does that make sense?
To extend that,
def pin_check
user = User.find_by_name('Crembo')
if user.pin == cookies[:pin]
'true'
else
'false'
end
end
returns true.
EDIT 3:
As asked in the comments, my User model:
class User < ActiveRecord::Base
has_many :posts
validates_uniqueness_of :name
def password_matches?(password_to_match)
self.password == Digest::SHA512.hexdigest(password_to_match)
end
end
Ok, apparently the problem was SQLite. For some reason it decided that it will not work properly.
I switched to MySQL and now everything works perfectly fine.