UTC +00:00], ["password", "abc123"], ["person_id", nil], ["updated_at", Tue, 31 Jul 2012 06:25:34 UTC +00:00], ["username", "joao"]](33.3ms) commit transactionPorem isto nao faz sentido no nosso modelo. Facamos a correcao:class UserAccount < ActiveRecord::Base attr_accessible :password, :username validates_presence_of :person, :username, :password belongs_to :personendAgora UserAccount passa se comportar conforme esperado:> u = UserAccount.create username: "a", password: "b"(0.2ms) begin transaction(0.1ms) rollback transaction> u.errors.messages=> {:person=>["can't be blank"]} Porem, isto tem um efeito colateral: ela afeta a criacao da user_account via nested attributes de Person:> p = Person.create name: "Jose da Silva", user_account_attributes: { username: "jsilva", password: "abc123" }(0.1ms) begin transaction(0.1ms) commit transaction(0.1ms) begin transaction(0.1ms) rollback transaction> p.errors.messages => {:"user_account.person"=>["can't be blank"]} Para evitar que ocorra a validacao quando sao usados nested attributes, deve-se informar para Person nao validar o atributo person da associacao user_account:class Person < ActiveRecord::Base attr_accessible :name, :user_account_attributes validates_presence_of :name has_one :user_account, inverse_of: :person accepts_nested_attributes_for :user_accountendUpdatePara uma operacao de atualizacao, e necessario passar em user_account_attributes o id da user_account previamente criada:> p.update_attributes user_account_attributes: { id:6, username: "jsilva11", password: "123456" }(0.1ms) begin transaction(0.6ms) UPDATE "user_accounts" SET "username" = 'jsilva11', "updated_at" = '2012-07-31 07:43:20.649645' WHERE "user_accounts"."id" = 6(49.9ms) commit transactionCUIDADO: caso o id fosse omitido, seria feito um novo insert na tabela user_accounts, e o objeto Person seria reassociado a nova user_account.DestroyPara completar este tutorial, suponhamos que faca sentido remover uma user_account, sem remover os respectivos dados de person. Isto pode ser obtido marcando o nested_model com allow_destroy:class Person < ActiveRecord::Base attr_accessible :name, :user_account_attributes validates_presence_of :name has_one :user_account, inverse_of: :person accepts_nested_attributes_for :user_account, allow_destroy: trueendAgora, pode-se remover user_account passando _destroy: true no hash user_account_attributes:> p.update_attributes user_account_attributes: { id:6, _destroy: true }(0.2ms) begin transactionUserAccount Load (0.1ms) SELECT "user_accounts".* FROM "user_accounts" WHERE "user_accounts"."person_id" = 7 LIMIT 1SQL (4.9ms) DELETE FROM "user_accounts" WHERE "user_accounts"."id" = ? [["id", 6]](47.8ms) commit transaction> p.reload.user_accountPerson Load (0.3ms) SELECT "people".* FROM "people" WHERE "people"."id" = ? LIMIT 1 [["id", 7]]UserAccount Load (1.9ms) SELECT "user_accounts".* FROM "user_accounts" WHERE "user_accounts"."person_id" = 7 LIMIT 1 => nil Referenciashttp://guides.rubyonrails.org/getting_started.html#building-a-multi-model-formhttp://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.htmlhttp://erikonrails.snowedin.net/?p=267http://archives.ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributeshttp://weblog.rubyonrails.org/2009/1/26/nested-model-forms/