Method: ActiveRecord::Base.object_id_attr
- Defined in:
- lib/active_record_extensions/active_record_extensions.rb
.object_id_attr(symbol, klass) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/active_record_extensions/active_record_extensions.rb', line 12 def self.object_id_attr(symbol, klass) module_eval %{ def #{symbol} if @#{symbol}.nil? or (@old_#{symbol}_id != #{symbol}_id) @old_#{symbol}_id = #{symbol}_id klass = #{klass}.descendants.find { |d| d.#{symbol}_name == #{symbol}_id } klass ||= #{klass} @#{symbol} = klass.new else @#{symbol} end end } end |