Skip to content

Instantly share code, notes, and snippets.

@colinramsay
Created February 24, 2010 01:41
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 colinramsay/312973 to your computer and use it in GitHub Desktop.
Save colinramsay/312973 to your computer and use it in GitHub Desktop.
class ActiveRecord::Base
def self.incorporates(association_id, options = {})
has_one(association_id)
id = association_id.to_s
klass = id.camelize.constantize
obj = klass.new
obj.attributes.each do |att|
send :define_method, "#{att}=" do |value|
assoc = send("#{association_id.to_s}")
unless assoc
assoc = klass.new
send("#{association_id.to_s}=",assoc)
end
assoc.send "#{att}=", value
end
send :define_method, "#{att}" do
assoc = send("#{association_id.to_s}")
unless assoc
assoc = klass.new
send("#{association_id.to_s}=",assoc)
end
return assoc.send "#{att}"
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment