如何解决使用连接的太阳黑子搜索关联
以下是模型列表及其关系:
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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。