Ruby on rails RubyonRails通过外键进行复杂搜索

Ruby on rails RubyonRails通过外键进行复杂搜索,ruby-on-rails,ruby,search,activerecord,Ruby On Rails,Ruby,Search,Activerecord,您好,在我的应用程序中,我正在尝试根据技能搜索一名员工。我有三个课程涉及员工、员工技能和技能 谁能告诉我如何进行搜索,因为我尝试过的每件事都返回了错误。这是他们的情态动词 雇员 class Employee < ActiveRecord::Base has_secure_password has_many :employee_events has_many :employee_skills , :class_name => EmployeeSkill, :foreign_key =

您好,在我的应用程序中,我正在尝试根据技能搜索一名员工。我有三个课程涉及员工、员工技能和技能

谁能告诉我如何进行搜索,因为我尝试过的每件事都返回了错误。这是他们的情态动词

雇员

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 EmployeeEmployeeSkill,:外键=>“员工id”
有很多:员工项目
有很多设备吗
有很多:时间
你有一个假期吗
有一个:角色
有一个:技能
有一个:税收
接受\u嵌套的\u属性\u:skill
验证:email,:presence=>true,:university=>true
验证:password,:confirmation=>true#password_confirmation attr
验证以下内容的长度:password,:in=>6..20,:on=>:create
def self.search(搜索)
如果搜索
其中('empLastName LIKE?',“%#{search}%”)
其他的
其中(:全部)
结束
结束
结束
员工技能

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 EmployeeSkillemployee,:foreign\u key=>employee\u id
属于:项目
属于:skill,:class\u name=>skill,:foreign\u key=>skill\u id
结束
技巧

class Skill < ActiveRecord::Base

has_many :employee_skills

def self.search(search)
  where("skillType LIKE ?", "%#{search}%")
end

end
职业技能
我认为你的情况很适合与很多人交往

class Employee < ActiveRecord::Base
 has_many :employee_skills
 has_many :skills, through: :employee_skills
end

class EmployeeSkills < ActiveRecord::Base
 belongs_to :employee
 belongs_to :skills
end

class Skill < ActiveRecord::Base
 has_many :employee_skills
 has_many :employees, through: :employee_skills
end

首先,无需指定
类别名称
外键
,因为
属于:employee
已指类别
employee
和外键
employee\u id

您可以使用
has\u和\u-beliens\u-to\u-many
关联,这符合您的需要:

class Employee
移民:

class CreateEmployeesSkills < ActiveRecord::Migration
  create_table :employees_skills, id: false do |t|
    t.belongs_to :employee, index: true
    t.belongs_to :skill, index: true
  end
end
class CreateEmployeesSkills
为什么需要一个技能类?当我可以通过employee___________________________________________。试试我的回答我得到了一个“没有找到‘skill’这个名字的关联。它已经被定义了吗?”错误,关于这个问题有什么建议吗?如果有一个原生的方法-
Has_和_belien_to _many
,为什么要使用这个呢?因为它是可扩展的,并且更符合OP的要求
class Employee < ActiveRecord::Base
  has_and_belongs_to_many :skills
end

class Skill < ActiveRecord::Base
  has_and_belongs_to_many :employees
end
class CreateEmployeesSkills < ActiveRecord::Migration
  create_table :employees_skills, id: false do |t|
    t.belongs_to :employee, index: true
    t.belongs_to :skill, index: true
  end
end