单表继承错误 - ActiveRecord :: SubclassNotFound

时间:2017-08-05 21:06:55

标签: ruby-on-rails single-table-inheritance

我的意图是实施两种类型的STI:员工和临床医师。我以前的实现是使用enums的角色,并且在尽力遵循类似问题的答案后,取出测试中的所有引用等来枚举角色并替换对类型的引用,当我获得以下错误时会出现以下错误的许多版本运行我的测试套件:

ERROR["test_valid_signup_information_with_account_activation", UsersSignupTest, 1.01794000000001]
 test_valid_signup_information_with_account_activation#UsersSignupTest (1.02s)
ActiveRecord::SubclassNotFound:         ActiveRecord::SubclassNotFound: The single-table inheritance mechanism failed to locate the subclass: 'Staff'. This error is raised because the column 'type' is reserved for storing the class in case of inheritance. Please rename this column if you didn't intend it to be used for storing the inheritance class or overwrite User.inheritance_column to use another column for that information.
            app/controllers/users_controller.rb:19:in `create'
            test/integration/users_signup_test.rb:27:in `block (2 levels) in <class:UsersSignupTest>'
            test/integration/users_signup_test.rb:26:in `block in <class:UsersSignupTest>'

以下几个方面让我感到困惑,可能会隐藏问题:

在我的用户模型user.rb中,我认为我正确定义了子类(工作人员和临床医师),但我不确定我是否正确包装了所有内容。是否所有其他代码都必须包含在其中一个类中?我在滥用&#34;结束&#34;?

class User < ApplicationRecord
end

class Staff < User
end

class Clinician < User
end

belongs_to :university
has_many :referral_requests




  attr_accessor :remember_token, :activation_token, :reset_token
  before_save   :downcase_email
  before_create :create_activation_digest
  validates :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 }
  validates :type, presence: true
  validates :university_id, presence: true, if: lambda { self.type == 'Staff' }

  has_secure_password
  validates :password, presence: true, length: { minimum: 6 }, allow_nil: true






  # Returns the hash digest of the given string.
  def User.digest(string)
    cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
                                                  BCrypt::Engine.cost
    BCrypt::Password.create(string, cost: cost)
  end

  # Returns a random token.
  def User.new_token
    SecureRandom.urlsafe_base64
  end

  # Remembers a user in the database for use in persistent sessions.
  def remember
    self.remember_token = User.new_token
    update_attribute(:remember_digest, User.digest(remember_token))
  end

  # Returns true if the given token matches the digest.
  def authenticated?(remember_token)
        return false if remember_digest.nil?
    BCrypt::Password.new(remember_digest).is_password?(remember_token)
  end


  # Forgets a user.
  def forget
    update_attribute(:remember_digest, nil)
  end

    # Returns true if the given token matches the digest.
  def authenticated?(attribute, token)
    digest = send("#{attribute}_digest")
    return false if digest.nil?
    BCrypt::Password.new(digest).is_password?(token)
  end

  # Activates an account.
  def activate
    update_attribute(:activated,    true)
    update_attribute(:activated_at, Time.zone.now)
  end

  # Sends activation email.
  def send_activation_email
    UserMailer.account_activation(self).deliver_now
  end

 # Sets the password reset attributes.
  def create_reset_digest
    self.reset_token = User.new_token
    update_attribute(:reset_digest,  User.digest(reset_token))
    update_attribute(:reset_sent_at, Time.zone.now)
  end

  # Sends password reset email.
  def send_password_reset_email
    UserMailer.password_reset(self).deliver_now
  end

   # Returns true if a password reset has expired.
  def password_reset_expired?
    reset_sent_at < 2.hours.ago
  end

 def feed
    ReferralRequest.where("user_id = ?", id)
  end


 private

    # Converts email to all lower-case.
    def downcase_email
    self.email = email.downcase
    end

    # Creates and assigns the activation token and digest.
    def create_activation_digest
      self.activation_token  = User.new_token
      self.activation_digest = User.digest(activation_token)
    end
end

这里是失败的特定测试代码(测试套件中许多失败的测试代码之一 - 但所有用户参数的定义都相似)。我是否适当地传递了工作人员参数?

 test "valid signup information with account activation" do
    get signup_path
    assert_difference 'User.count', 1 do
      post users_path, params: { user: { name:  "Example User",
                                         email: "user@example.com",
                                         university_id: 1 ,
                                         type: "Staff",
                                         password:              "password",
                                         password_confirmation: "password" } }

这是我的用户表架构:

create_table "users", force: :cascade do |t|
    t.string   "name"
    t.string   "email"
    t.datetime "created_at",                        null: false
    t.datetime "updated_at",                        null: false
    t.string   "password_digest"
    t.string   "remember_digest"
    t.string   "activation_digest"
    t.boolean  "activated",         default: false
    t.datetime "activated_at"
    t.string   "reset_digest"
    t.datetime "reset_sent_at"
    t.integer  "university_id"
    t.integer  "role"
    t.string   "type"
    t.index ["email"], name: "index_users_on_email", unique: true
  end

非常感谢任何想法!我在这里问了很多问题,但只是在尝试了很长一段时间后才能解决类似问题。

1 个答案:

答案 0 :(得分:1)

假设上面的代码示例是准确的,您会看到此错误,因为user.rb文件是无效的Ruby并且无法解析。您还应该看到解释器错误。

class User < ApplicationRecord
  belongs_to :university
  has_many :referral_requests

  attr_accessor :remember_token, :activation_token, :reset_token
  before_save   :downcase_email
  before_create :create_activation_digest
  validates :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 }
  validates :type, presence: true

  has_secure_password
  validates :password, presence: true, length: { minimum: 6 }, allow_nil: true
  # etc...
end

class Staff < User
  validates :university_id, presence: true
end

class Clinician < User
end

标准类继承实践适用,因此如果其中的代码仅适用于特定的子类,则应移动到那里(例如university_id验证移至Staff)。

# Returns the hash digest of the given string.
def User.digest(string)
  cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
                                              BCrypt::Engine.cost
  BCrypt::Password.create(string, cost: cost)
end

# Returns a random token.
def User.new_token
  SecureRandom.urlsafe_base64
end

这些应该写成

def self.digest(string)
  # ...
end

def self.new_token
  # ...
end

或者,

class << self
  def digest(string)
    # ...
  end

  def new_token
    # ...
  end
end