您能帮助我理解为什么此布尔方法返回false吗?

时间:2018-07-02 05:31:45

标签: ruby-on-rails boolean-expression

我有一个带account_type属性的“用户”或“合作伙伴”用户模型。我在用户模型中创建了布尔方法,以确定用户记录是学生还是合伙人(请参见下文)。

 def student?
    self.account_type == "Student"
  end

  def partner?
    self.account_type == "Partner"
  end

在Rails控制台中,当我将user设置为具有学生帐户类型的User实例并输入user.account_type ==“ Student”时,我为true,但是当我调用user.student?时,我为false。我如何设置这些方法是否有问题?它们看起来很简单,所以我不理解为什么记录没有返回true。

控制台输出:

user = User.last
#<User id: 18, first_name: "gjalrgkj", last_name: "kgjrlgakjrl", email: "terajglrkj@gmail.com", password_digest: "$2a$10$WF.Rw3PzlWilH0X.Nbfxfe5aB18WW6J7Rt4SAKQEwI8...", remember_digest: nil, activation_digest: "$2a$10$/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 个答案:

答案 0 :(得分:3)

您的2种方法partner?user.student?属于用户模型。

在执行student?时,它将在用户模型中寻找self方法作为实例方法。

帮助器中的

app.service('searchService', function() { this.searchState={ loading: false, data: null, error: null } this.fetchSearchResults = function(key){ // call api methods to get response // can be via callbacks or promise. this.searchState.loading=true; someMethodThatCallsApi(key) .then(function(success){ this.searchState.loading=false; this.searchState.data=success; this.searchState.error=null }) .catch(function(error){ this.searchState.loading=false; this.searchState.data=null; this.searchState.error=error }); } this.getState = function(){ return this.searchState } }); 不是它指向您的帮助器模块的用户实例。

希望这会有所帮助。

答案 1 :(得分:0)

尝试将此代码放入用户模型(不在Helper中)

def student?
  self.account_type == "Student"
end

def partner?
  self.account_type == "Partner"
end

这是一个创建设置器以设置account_type(例如)的好主意

def set_account_type(type)
  self.account_type = type
end