你能帮我理解为什么这个布尔方法返回 false 吗?
Can you help me understand why this boolean method is returning false?
我有一个 User 模型,其 account_type 属性为 "Student" 或 "Partner"。我在我的用户模型中创建了布尔方法来确定用户记录是学生还是合作伙伴(见下文)。
def student?
self.account_type == "Student"
end
def partner?
self.account_type == "Partner"
end
在 rails 控制台中,当我将 user 设置为具有学生帐户类型的 User 实例并输入 user.account_type == "Student" 时,我得到了 true,但是当我调用 user.student?,我错了。我如何设置这些方法有问题吗?它们看起来非常简单,所以我不明白为什么没有返回 true 以供记录。
控制台输出:
user = User.last
#<User id: 18, first_name: "gjalrgkj", last_name: "kgjrlgakjrl", email: "terajglrkj@gmail.com", password_digest: "a$WF.Rw3PzlWilH0X.Nbfxfe5aB18WW6J7Rt4SAKQEwI8...", remember_digest: nil, activation_digest: "a$/bXG4/nKCiiZHWailUPAmOZj7YhCjKhPm4lUW6nPC3N...", activated: nil, activated_at: nil, reset_digest: nil, reset_sent_at: nil, account_type: "Student", created_at: "2018-07-02 04:21:07", updated_at: "2018-07-02 04:21:07">
>> user.account_type
=> "Student"
>> user.account_type = "Student"
=> "Student"
>> user.student?
=> false
用户模型:
class User < ApplicationRecord
has_one :personal_information
attr_accessor :remember_token, :activation_token, :reset_token
before_save :downcase_email
before_create :create_activation_digest
validates :first_name, presence: true, length: { maximum: 50 }
validates :last_name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
has_secure_password
validates :password, presence: true, length: { minimum: 6 }, allow_nil: true
validates :account_type, presence: true
def User.new_token
SecureRandom.urlsafe_base64
end
def User.digest(string)
cost = ActiveModel::SecurePassword.min_cost BCrypt::Engine::MIN_COST :
BCrypt::Engine.cost
BCrypt::Password.create(string, cost: cost)
end
def create_reset_digest
self.reset_token = User.new_token
update_columns(reset_digest: User.digest(reset_token), reset_sent_at: Time.zone.now)
end
def authenticated?(attribute, token)
return false if digest.nil?
BCrypt::Password.new(digest).is_password?(token)
end
def remember
self.remember_token = User.new_token
update_attribute(:remember_digest, User.digest(remember_token))
end
def forget
update_attribute(:remember_digest, nil)
end
def provide_age
now = Time.now.utc.to_date
if self.birthday.nil?
nil
else
self.age = now.year - self.birthday.year - ((now.month > self.birthday.month || (now.month == self.birthday.month && now.day >= self.birthday.day)) ? 0 : 1)
update_attribute(:age, self.age)
end
end
def send_activation_email
UserMailer.account_activation(self).deliver_now
end
def activate
update_columns(activated: true, activated_at: Time.zone.now)
end
def send_password_reset_email
UserMailer.password_reset(self).deliver_now
end
def password_reset_expired?
reset_sent_at < 2.hours.ago
end
private
def downcase_email
self.email.downcase!
end
def create_activation_digest
self.activation_token = User.new_token
self.activation_digest = User.digest(activation_token)
end
end
用户助手
def account_type
[
['Student'],
['Partner'],
['School Administrator'],
['Philanthropist']
]
end
def student?
self.account_type == "Student"
end
def partner?
self.account_type == "Partner"
end
end
您的 2 个方法 student?
和 partner?
属于用户模型。
当您执行 user.student?
时,它会在用户模型中查找 student?
方法作为实例方法。
self
中的 helper 不是它指向你的 helper 模块的用户实例。
希望这对您有所帮助。
尝试将此代码放入您的用户模型(而不是助手)
def student?
self.account_type == "Student"
end
def partner?
self.account_type == "Partner"
end
创建一个 setter 来设置 account_type 是个好主意,例如
def set_account_type(type)
self.account_type = type
end
我有一个 User 模型,其 account_type 属性为 "Student" 或 "Partner"。我在我的用户模型中创建了布尔方法来确定用户记录是学生还是合作伙伴(见下文)。
def student?
self.account_type == "Student"
end
def partner?
self.account_type == "Partner"
end
在 rails 控制台中,当我将 user 设置为具有学生帐户类型的 User 实例并输入 user.account_type == "Student" 时,我得到了 true,但是当我调用 user.student?,我错了。我如何设置这些方法有问题吗?它们看起来非常简单,所以我不明白为什么没有返回 true 以供记录。
控制台输出:
user = User.last
#<User id: 18, first_name: "gjalrgkj", last_name: "kgjrlgakjrl", email: "terajglrkj@gmail.com", password_digest: "a$WF.Rw3PzlWilH0X.Nbfxfe5aB18WW6J7Rt4SAKQEwI8...", remember_digest: nil, activation_digest: "a$/bXG4/nKCiiZHWailUPAmOZj7YhCjKhPm4lUW6nPC3N...", activated: nil, activated_at: nil, reset_digest: nil, reset_sent_at: nil, account_type: "Student", created_at: "2018-07-02 04:21:07", updated_at: "2018-07-02 04:21:07">
>> user.account_type
=> "Student"
>> user.account_type = "Student"
=> "Student"
>> user.student?
=> false
用户模型:
class User < ApplicationRecord
has_one :personal_information
attr_accessor :remember_token, :activation_token, :reset_token
before_save :downcase_email
before_create :create_activation_digest
validates :first_name, presence: true, length: { maximum: 50 }
validates :last_name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
has_secure_password
validates :password, presence: true, length: { minimum: 6 }, allow_nil: true
validates :account_type, presence: true
def User.new_token
SecureRandom.urlsafe_base64
end
def User.digest(string)
cost = ActiveModel::SecurePassword.min_cost BCrypt::Engine::MIN_COST :
BCrypt::Engine.cost
BCrypt::Password.create(string, cost: cost)
end
def create_reset_digest
self.reset_token = User.new_token
update_columns(reset_digest: User.digest(reset_token), reset_sent_at: Time.zone.now)
end
def authenticated?(attribute, token)
return false if digest.nil?
BCrypt::Password.new(digest).is_password?(token)
end
def remember
self.remember_token = User.new_token
update_attribute(:remember_digest, User.digest(remember_token))
end
def forget
update_attribute(:remember_digest, nil)
end
def provide_age
now = Time.now.utc.to_date
if self.birthday.nil?
nil
else
self.age = now.year - self.birthday.year - ((now.month > self.birthday.month || (now.month == self.birthday.month && now.day >= self.birthday.day)) ? 0 : 1)
update_attribute(:age, self.age)
end
end
def send_activation_email
UserMailer.account_activation(self).deliver_now
end
def activate
update_columns(activated: true, activated_at: Time.zone.now)
end
def send_password_reset_email
UserMailer.password_reset(self).deliver_now
end
def password_reset_expired?
reset_sent_at < 2.hours.ago
end
private
def downcase_email
self.email.downcase!
end
def create_activation_digest
self.activation_token = User.new_token
self.activation_digest = User.digest(activation_token)
end
end
用户助手
def account_type
[
['Student'],
['Partner'],
['School Administrator'],
['Philanthropist']
]
end
def student?
self.account_type == "Student"
end
def partner?
self.account_type == "Partner"
end
end
您的 2 个方法 student?
和 partner?
属于用户模型。
当您执行 user.student?
时,它会在用户模型中查找 student?
方法作为实例方法。
self
中的 helper 不是它指向你的 helper 模块的用户实例。
希望这对您有所帮助。
尝试将此代码放入您的用户模型(而不是助手)
def student?
self.account_type == "Student"
end
def partner?
self.account_type == "Partner"
end
创建一个 setter 来设置 account_type 是个好主意,例如
def set_account_type(type)
self.account_type = type
end