使用连接的太阳黑子搜索关联

问题描述

以下是模型列表及其关系:

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,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,optional: false
end

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

解决方法

在我的 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,to: :ref_id})
   end
end

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...