Skip to content

Instantly share code, notes, and snippets.

@bensonk
Created October 15, 2011 01:59
Show Gist options
  • Save bensonk/1288885 to your computer and use it in GitHub Desktop.
Save bensonk/1288885 to your computer and use it in GitHub Desktop.
module AssociationIds
def ids_for name
name = name.to_s
define_method (name + '_ids=').to_sym do |ids|
values = ids.collect { |id| Kernel.const_get(name.classify).find id }
self.send(name + '=', values)
end
define_method (name + '_ids').to_sym do
self.send(name).collect { |obj| obj.id }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment