使用ROR和Postgresql进行搜索

siv3szwd  于 2022-12-12  发布在  PostgreSQL
关注(0)|答案(1)|浏览(89)

I want to implement search in my ROR application. I have module

class Employee < ApplicationRecord
    belongs_to :parent, class_name: 'Employee'
end

The employee has fields like:

name: string
employee_id: number

The Serializer looks like:

class EmployeeSerializer < ApplicationSerializer
  # include FastJsonapi::ObjectSerializer
  attributes :name, employee_id, :is_boss, :boss

  def boss
    object.parent.nil? ? {} : ActiveModel::SerializableResource.new(object.parent)
  end
end

A employee will have its own boss. i.e. when is serialize it shows like

{
   employee: {
      name: "employee_name",
      employee_id: 123,
      is_boss: false,
      boss: {
         employee: {
             name: "boss name",
             employee_id: 1010
         }
      }
   }
}

what happens is when is search from the query Employee.where("employee_id iLike ?", 1212) its working but i wanted to search based on two thing where it check on employee_id and employes associated boss'es employee_id.
I tried

Employee.where("CONCAT_WS(
            ' ',
            employee_id,
            parent.employee_id
            )
            iLIKE ?",
      "%#{params[:query]&.squish}%"

But getting issue on parent.employee_id
Example:

if i query Employee.find(1).parent it gives the boss information
Thanks

omjgkv6w

omjgkv6w1#

我想您正在寻找:

boss_id = Employee.where(employee_id: 2).id

Employee.where(employee_id: 1212, parent_id: boss_id)

相关问题