Module: AutoBuild::Builder::ClassMethods
- Defined in:
- lib/auto_build/builder.rb
Instance Method Summary collapse
-
#auto_build(*attr_names) ⇒ Object
Public: This method allows you to auto initialize associations in your models.
Instance Method Details
#auto_build(*attr_names) ⇒ Object
Public: This method allows you to auto initialize associations in your models. After calling it you don’t need to call ‘build_association` or `association.build` in your controllers or views. Useful for models that accept nested attributes for associations. It **does not** overwrite associations with existing values.
An ‘after_initialize` hook will be created for each one of the associations.
Signature
auto_build(<field>, <field>, <field>)
auto_build(<field>, <options>)
field - A Symbol with the name of the association you want
initialize
options - A Hash with the options to initialize the
associations. See AutoBuilder::Association#initialize
for the options.
Examples
class User
has_one :address
has_many :pictures
has_many :projects
end
auto_build :address, :pictures
# => Builds a record for each association if none is present
auto_build :projects, :count => 3
# => Builds 3 projects each time you initialize a User
auto_build :pictures, :append => true
# => Builds an extra Picture each time you initialize a User
43 44 45 46 47 48 49 50 |
# File 'lib/auto_build/builder.rb', line 43 def auto_build(*attr_names) = attr_names. names = attr_names names.map do |name| Association.new(self, name, ).add_hook end end |