简体   繁体   中英

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.

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.

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

I found a solution that seems to be working well, I added a named scope on the artist class:

named scope :agencies include, lambda { |c| { :joins=> :agents,:conditions => { :agents users => { :company => c } } } }

The search field is now called search[artist agencies include]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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