【发布时间】:2017-07-30 05:44:53
【问题描述】:
有多对多关系,但不断收到相同的错误:ActiveModel::UnknownAttributeError: EmployeeSkill 的未知属性“employee_id”。我检查了我的两个模型,似乎我都有 ID,所以不知道该怎么办?
class Skill < ApplicationRecord
validates :skill_name, presence: true, length: { minimum: 3, maximum: 30}
validates_uniqueness_of :skill_name
has_many :employee_skills
has_many :employees, through: :employee_skills
end
...
class Employee < ApplicationRecord
before_save { self.email = email.downcase }
#validates :email, presence: true, length: { maximum: 30 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
has_many :employee_skills
has_many :skills, through: :employee_skills
end
...
class EmployeeSkill < ApplicationRecord
belongs_to :employee
belongs_to :skill
end
【问题讨论】:
-
检查您的
schema.rb,EmployeeSkill 是否有employee_id属性?