Module: Sequel::Plugins::NestedAttributes::ClassMethods

Defined in:
lib/sequel/lib/sequel/plugins/nested_attributes.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#nested_attributes_moduleObject

Module to store the nested_attributes setter methods, so they can call be overridden and call super to get the default behavior


26
27
28
# File 'lib/sequel/lib/sequel/plugins/nested_attributes.rb', line 26

def nested_attributes_module
  @nested_attributes_module
end

Instance Method Details

#nested_attributes(*associations, &block) ⇒ Object

Allow nested attributes to be set for the given associations. Options:

  • :destroy - Allow destruction of nested records.

  • :limit - For *_to_many associations, a limit on the number of records that will be processed, to prevent denial of service attacks.

  • :remove - Allow disassociation of nested records (can remove the associated object from the parent object, but not destroy the associated object).

  • :strict - Set to false to not raise an error message if a primary key is provided in a record, but it doesn't match an existing associated object.

If a block is provided, it is passed each nested attribute hash. If the hash should be ignored, the block should return anything except false or nil.


40
41
42
43
44
45
46
47
48
49
# File 'lib/sequel/lib/sequel/plugins/nested_attributes.rb', line 40

def nested_attributes(*associations, &block)
  include(self.nested_attributes_module ||= Module.new) unless nested_attributes_module
  opts = associations.last.is_a?(Hash) ? associations.pop : {}
  reflections = associations.map{|a| association_reflection(a) || raise(Error, "no association named #{a} for #{self}")}
  reflections.each do |r|
    r[:nested_attributes] = opts
    r[:nested_attributes][:reject_if] ||= block
    def_nested_attribute_method(r)
  end
end