Skip to content

Instantly share code, notes, and snippets.

@colinramsay colinramsay/inc.rb
Created Feb 24, 2010

Embed
What would you like to do?
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
You can’t perform that action at this time.