【问题标题】:sunspot search assiocation using joins使用连接的太阳黑子搜索关联
【发布时间】:2021-06-16 10:52:25
【问题描述】:

下面是模型及其关系的列表:

class Section
  has_many :students, as: :resource

  searchable do 
    integer :id
    join(:first_name, prefix: "student", target: Student, type: :text, join: {from: :resource_id, to: :id})
    join(:last_name, prefix: "student", target: Student, type: :text, join: {from: :resource_id, to: :id})
   end
end

class Student
  belongs_to :resource, polymorphic: true, optional: false

  has_many :contact_number, as: :resource

  searchable do
    
    text :first_name
    text :last_name

    integer :id
    integer :resource_id


    string :first_name
    string :last_name

  end
end


class ContactNumber
  belongs_to :resource, polymorphic: true, optional: false
end

正如您在我的班级模型中看到的那样,部分有很多学生。由于加入的帮助,我可以搜索学生“first_name”和学生“last_name”。有没有可能搜索学生联系电话的方法。使用连接???或者在 Section 模型中搜索联系人号码的解决方法是什么?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-5 sunspot sunspot-rails


    【解决方案1】:

    在我的 ContactNumber 模型中,我创建了另一个联接

    class ContactNumber
      searchable do
        string :ref_id do
          if resource_type == "Student"
            [resource.resource_type, resource.resource_id].join("_").downcase()
          end
       end
    end
    

    在我的学生模型中

    searchable do 
      string :ref_id do
      [resource_type, resource_id].join("_").downcase()
    end
    
      join(:content, prefix: "contact_number", target: ContactNumber, type: :text, join: {from: :ref_id, to: :ref_id})
    end
    

    在我的 Section 课程中排名最后

    class Section 
       searchable do
        string :ref_id do
      [self.class.name, id].join("_").downcase()
    end
    join(:content, prefix: "number", target: ContactPerson, type: :text, join: {from: :ref_id, to: :ref_id})
       end
    end
    

    【讨论】:

      猜你喜欢
      • 2014-05-30
      • 2015-01-23
      • 2012-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-14
      • 1970-01-01
      相关资源
      最近更新 更多