Skip to content

Instantly share code, notes, and snippets.

@dpmcnevin
Created September 7, 2010 00:32
Show Gist options
  • Save dpmcnevin/567694 to your computer and use it in GitHub Desktop.
Save dpmcnevin/567694 to your computer and use it in GitHub Desktop.
class OperatingSystem < ActiveRecord::Base
belongs_to :host
def self.search(term)
t = OperatingSystem.arel_table
match_on = [:name,:version,:service_pack]
matches = match_on.map {|m| "IF(#{t[m].matches("%#{term}%").to_sql},operating_systems.#{m.to_s},NULL)" }
matching_string = "CONCAT_WS(' ', #{matches.join(",")}) as matched_on"
results = OperatingSystem.select(
"*",
matching_string
).having("matched_on != ''").joins(:host)
results.map do |os|
host = os.host
host.matched_on ||= []
host.matched_on << os.matched_on
host
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment