简体   繁体   English

SearchLogic + STI

[英]SearchLogic + STI

Trying to implement a search logic search that uses associations with STI but I'm having a problem where it is not select the STI records as the subclass but the parent. 尝试实现使用与STI关联的搜索逻辑搜索,但是我遇到的一个问题是它不是选择STI记录作为子类而是父类。

Example: 例:


class Users
end

class Artist < User has many :agents, :through => :agents artists end

class Agent < User has many :artists, :through => :agents artists end

when i do a search for "artist agents company like", it is searching based on the agents as users rather than as agents: 当我搜索“像这样的艺术家代理公司”时,它是基于作为用户而不是代理的代理进行搜索的:

select * from users WHERE users.company LIKE

rather than 而不是

select * from users AS agents WHERE agents.company LIKE

Wondering if I can pre-empt this problem at the ActiveRecord class level (eg in the association I was thinking that if you could specify that the agents would get loaded :as=>:agent or something along those lines), or if I would need to patch searchlogic or what else I could do to accomplish this. 想知道我是否可以在ActiveRecord类级别上抢先解决这个问题(例如,在关联中,我在想如果您可以指定将加载代理:as =>:agent或类似的东西),或者我是否愿意需要修补searchlogic或我可以做些什么来完成此操作。

One other option that occurred to me, and I dread the idea, is to add a field on the user table that includes a listing of the person's agencies. 我不喜欢这个想法,我想到的另一个选择是在用户表上添加一个字段,其中包含此人的代理机构列表。 eg users.agencies => Agency One Name,Agency Two Name 例如users.agencies =>代理商一个名称,代理商两个名称

I found a solution that seems to be working well, I added a named scope on the artist class: 我找到了一个似乎运行良好的解决方案,我在artist类上添加了一个命名范围:

named scope :agencies include, lambda { |c| 命名范围:代理商包括lambda {| c | { :joins=> :agents,:conditions => { :agents users => { :company => c } } } } {:joins =>:agents,:conditions => {:agents用户=> {:company => c}}}}

The search field is now called search[artist agencies include] 搜索字段现在称为搜索[艺术家机构包括]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM