Skip to content

Instantly share code, notes, and snippets.

@steveklabnik
Created September 25, 2013 20:52
Show Gist options
  • Save steveklabnik/6705873 to your computer and use it in GitHub Desktop.
Save steveklabnik/6705873 to your computer and use it in GitHub Desktop.
def self.search(query)
query
.split(" ")
.collect do |query|
query.split(":")
end.inject(self) do |klass, (name, q)|
association = klass.reflect_on_association(name.pluralize.to_sym)
association_table = association.klass.arel_table
if [:has_and_belongs_to_many, :belongs_to].include?(association.macro)
joins(name.pluralize.to_sym).where(association_table["name"].eq(q))
else
all
end
end
end
@steveklabnik
Copy link
Author

@soulcutter, in Rails 4, all just returns a scope. The naming is better that way, though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment