Module: NestedAttributeHelper

Defined in:
lib/nested_attribute_helper.rb

Instance Method Summary collapse

Instance Method Details

#init_nested_attributes(reflections) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/nested_attribute_helper.rb', line 4

def init_nested_attributes(reflections)
    reflections.each do |r|
        define_method("#{r}_attributes=") do |attr|
            if attr.present?
                children = []
                attr.each do |child|
                    children << r.classify.constantize.find_or_initialize_by(child)
                end
                eval("self.#{r} = children")
            end
        end
    end
end