【发布时间】:2017-08-25 10:54:32
【问题描述】:
您好,在我的应用程序中,我正在尝试根据技能搜索员工。我有 3 个课程涉及 Employee、Employee_skills 和 Skills
任何人都可以指出如何进行此搜索的方向,因为我尝试过的所有内容都返回错误。这是他们的模态。
员工
class Employee < ActiveRecord::Base
has_secure_password
has_many :employee_events
has_many :employee_skills , :class_name => EmployeeSkill, :foreign_key => "employee_id"
has_many :employee_projects
has_many :equipments
has_many :time_entry
has_one :holiday
has_one :role
has_one :skill
has_one :tax
accepts_nested_attributes_for :skill
validates :email, :presence => true, :uniqueness => true
validates :password, :confirmation => true #password_confirmation attr
validates_length_of :password, :in => 6..20, :on => :create
def self.search(search)
if search
where('empLastName LIKE ?', "%#{search}%")
else
where(:all)
end
end
end
员工技能
class EmployeeSkill < ActiveRecord::Base
belongs_to :employee, :class_name => Employee, :foreign_key => "employee_id"
belongs_to :project
belongs_to :skill, :class_name => Skill, :foreign_key => "skill_id"
end
技能
class Skill < ActiveRecord::Base
has_many :employee_skills
def self.search(search)
where("skillType LIKE ?", "%#{search}%")
end
end
【问题讨论】:
-
为什么需要一个技能类?
-
当我可以通过employee_skills将许多员工分配给该技能时,我可以独立添加一项新技能
-
这就是为什么你需要一个 has_many through 关系。试试我的答案
标签: ruby-on-rails ruby search activerecord