Skip to content

Instantly share code, notes, and snippets.

@klochner
Created February 5, 2010 00:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klochner/295318 to your computer and use it in GitHub Desktop.
Save klochner/295318 to your computer and use it in GitHub Desktop.
class << Foo
def mass_habtm(attr_array)
attr_str = attr_array.map{|a| %Q{'#{a}'} }.uniq.join(",")
self.connection.execute(%Q{insert into foos_bars (foo_id,bar_id)
select distinct foos.id,bars.id from foos,bars
where foos.id = #{self.id}
and bars.some_attribute in (#{attr_str})})
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment