Module: ActiveObject::Associations::HasOneAssociation::ClassMethods
- Defined in:
- lib/active_object/associations.rb
Instance Method Summary collapse
-
#has_one(association, options = {}) ⇒ Object
选项示例: has_one :icon,:class_name=>“Photo”.
Instance Method Details
#has_one(association, options = {}) ⇒ Object
选项示例: has_one :icon,:class_name=>“Photo”
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/active_object/associations.rb', line 48 def has_one(association, = {}) class_name = .delete(:class_name) || association.to_s.camelize write_inheritable_attribute "#{association}_class",class_name # 保存关联的类名 attr_accessor "#{association}" merge_attributes "#{association}_id" # 将关联id作为持久化属性 define_method("#{association}_id") do self.instance_variable_get("@#{association}") ? self.instance_variable_get("@#{association}").id : nil end define_method("#{association}_id=") do |value| self.instance_variable_set("@#{association}",self.class.read_inheritable_attribute("#{association}_class").constantize.find(value)) end end |