0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ActiveModel::UnknownAttributeError: unknown attribute 'password' for StaffMember.

Posted at

bin/rails db:migrateコマンドを入力するとターミナルで以下のようなエラー文が。

Creating staff_members....
rails aborted!
ActiveModel::UnknownAttributeError: unknown attribute 'password' for StaffMember.

StaffMemberのpasswordが存在しないよと仰ってます。
最初にmigrationファイルを疑ってかかるも、passwordに関する記述は間違っていませんでした。続いてモデルを見に行くことに。

staff_member.rb
class StaffMember < ApplicationRecord
  include EmailHolder
  include PersonalNameHolder
  has_many :events, class_name:"StaffEvent", dependent: :destroy

  
  
  validates :start_date ,presence: true , date:{
    after_or_equal_to: Date.new(2000,1,1),
    before: -> (obj) { 1.year.from_now.to_date },
    allow_blank: true
  }
  validates :end_date, date: {
    after: :start_date,
    before: -> (obj) { 1.year.from_now.to_date },
    allow_blank: true
  }
  
  def active?
    !suspended && start_date <= Date.today &&
      (end_date.nil? || end_date > Date.today)
  end
end

PasswordHolderというmoduleを継承していないことが判明しました。

staff_member.rb
class StaffMember < ApplicationRecord
  include EmailHolder
  include PersonalNameHolder
  include PasswordHolder            <=ここを追記
  has_many :events, class_name:"StaffEvent", dependent: :destroy

  
  
  validates :start_date ,presence: true , date:{
    after_or_equal_to: Date.new(2000,1,1),
    before: -> (obj) { 1.year.from_now.to_date },
    allow_blank: true
  }
  validates :end_date, date: {
    after: :start_date,
    before: -> (obj) { 1.year.from_now.to_date },
    allow_blank: true
  }
  
  def active?
    !suspended && start_date <= Date.today &&
      (end_date.nil? || end_date > Date.today)
  end
end

エラーが解消しました。

bash-4.4$ bin/rails db:reset
Dropped database 'baukis2_development'
Dropped database 'baukis2_test'
Created database 'baukis2_development'
Created database 'baukis2_test'
Creating staff_members....
Creating administrators....
Creating staff_events....
Creating customers....
bash-4.4$ 
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?