instance method generate_association_writer

Ruby on Rails 4.2.9

Since v4.0.13 Private

Available in: v4.0.13 v4.1.16 v4.2.9 v5.2.8.1 v6.0.6 v6.1.7.10 v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

generate_association_writer(association_name, type)

Generates a writer method for this association. Serves as a point for accessing the objects in the association. For example, this method could generate the following:

def pirate_attributes=(attributes)
  assign_nested_attributes_for_one_to_one_association(:pirate, attributes)
end

This redirects the attempts to write objects in an association through the helper methods defined below. Makes it seem like the nested associations are just regular associations.

Parameters

association_name req
type req
Source
# File activerecord/lib/active_record/nested_attributes.rb, line 337
      def generate_association_writer(association_name, type)
        generated_association_methods.module_eval <<-eoruby, __FILE__, __LINE__ + 1
          if method_defined?(:#{association_name}_attributes=)
            remove_method(:#{association_name}_attributes=)
          end
          def #{association_name}_attributes=(attributes)
            assign_nested_attributes_for_#{type}_association(:#{association_name}, attributes)
          end
        eoruby
      end

Defined in activerecord/lib/active_record/nested_attributes.rb line 337 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveRecord::NestedAttributes::ClassMethods

Type at least 2 characters to search.

↑↓ navigate · open · esc close