I am trying to create persistante variable local to my model but although something that sounds so simple is not working.
I have this in my model:
class Coupon < ActiveRecord::Base
@username = "empty"
@admin = false
def self.setUser(name, isAdmin)
@username = name
@admin = isAdmin
end
def self.get_user (user)#an attempt to access the current_user but did not work i call this from the controller (I understand it is not best practice)
#@user = user
self.setUser(user.username,user.admin?)
end
def has_not_occurred
errors.add("property_of","name is not valid:#{@username}") if !validPropertyOf?
end
end
def validProperty_of?
return property_of == @username # || @Admin
end
end
I actually get a “” instead of “empty” or the new value of username in set.user. How do I make these values persist? I have printed the values inside each method so they persist inside the method but not beyond for some reason.
@username is always nil or “” when it gets to has_not_accurred.
Why is this and how do I make it persist? Thank you so much.
I cannont access @user when I set it either (get_user method). I get a nil instance later down at validateProperty_of
Upgraded Rails now it works. Not sure why.