-
-
Save steveklabnik/7087076 to your computer and use it in GitHub Desktop.
class Foo | |
has_many :tags | |
has_many :states | |
# q is hash: { "tags" => "ruby", "states" => "open" } | |
def self.search(q) | |
# wat do? | |
end | |
end | |
# search should return a relation that scopes everything to the terms | |
# the values 'ruby' and 'open' are in the 'name' attribute of the Tag and State model. |
We typically avoid joins and just do an additional query (joins are expensive in our data set). Building on the scope example:
scope :by_tag, ->(*tags) {
ids = Tag.where(:name => tags.flatten).uniq.pluck(:foo_id)
where(:id => ids)
}
scope :by_state, ->(*states) {
ids = State.where(:name => states.flatten).uniq.pluck(:foo_id)
where(:id => ids)
}
def self.search(query)
records = all
records = records.by_tags(query["tags"].split(' ')) if query["tags"].present?
records = records.by_states(query["states"].split(' ')) if query["states"].present?
records
end
If joins aren't a concern, then this could be easily tweaked, but I prefer the pluck
pattern.
@coreyword yes, the real search method takes a string, and i do some processing to get it into the hash form. figured i'd just ignore those details here though.
Thanks so much everyone!
I'd love to know what you settle on. In a system of mine I do something like @subdigital's solution...
I prefer what you did @ https://gist.github.com/steveklabnik/6705873
I've tried like 20 different things with search module and stuff in the past year and never was satisfied by it. I think searching would be much easier to build in an abstract way if it would be possible to list the scopes for an ActiveRecord::Base. Is there a reason why it's not available?
shot in the dark. didn't run it