Plugin allowing to auto build and validate associated records and merge validation errors
== Examples
If you want automatically build specified associated recrods when new record is
initialized you can use auto_build:
class Bar < ActiveRecord::Base
belongs_to :foo
end
class Foo < ActiveRecord::Base
has_one :bar
set_associated_records_to_auto_build :bar
end
foo = Foo.new
foo.bar # => #Bar:0x...
You can also validate associated record in easy way:
class Person < ActiveRecord::Base
has_one :address
validates_presence_of :name
validates_associated_record :address
delegate :street, :street=, :city, :city=, :state, :state=,
:zip, :zip=, :to => :address
end
class Address < ActiveRecord::Base
validates_presence_of :street, :city, :state, :zip
end
joe = Person.new
joe.valid? # => false
joe.errors.on(:name) # => "can't be blank"
joe.errors.on(:street) # => "can't be blank"
joe.errors.on(:city) # => "can't be blank"
joe.errors.on(:state) # => "can't be blank"
joe.errors.on(:zip) # => "can't be blank"
And of course merging errors in not associated records:
class Person < ActiveRecord::Base
validates_presence_of :name
end
class Address < ActiveRecord::Base
validates_presence_of :street, :city, :state, :zip
end
joe = Person.new
joe.valid? # => false
address = Address.new
address.valid? # => false
joe.errors.merge address.errors
joe.errors.on(:name) # => "can't be blank"
joe.errors.on(:street) # => "can't be blank"
joe.errors.on(:city) # => "can't be blank"
joe.errors.on(:state) # => "can't be blank"
joe.errors.on(:zip) # => "can't be blank"
Enjoy!