Este post e o segundo de uma serie de quatro artigos:Parte 1: Nested Models - One-to-OneParte 2: Nested Models - One-to-ManyParte 3: Nested FormsParte 4: Nested Forms dinamicos usando Knockout.jsO codigo desta aplicacao de exemplo esta disponivel neste repositorio Github.Nested Models - One-to-ManySegue o modelo utilizado neste tutorial:Person has one UserAccount (parte 1)UserAccount has many Permissions (parte 2)Conforme estabelecido na parte 1 desta serie, queremos editar UserAccount a partir do model Person. Nesta segunda parte, veremos como editar tambem a partir do model Person a associacao one-to-many UserAccount / Permissions. O modelo de permissoes sera bem simples:rails g model Permission user_account_id:integer restricted_area:string grants:stringrake db:migratePor fim, resta configurar as validacoes e relacionamentos nos respectivos models.Nested One-to-ManyE necessario adicionar uma clausula has_many em UserAccount, para que esta possa navegar na associacao um-para-muitos. Permission possui a chave estrangeira user_account_id (lado belongs_to). Por fim, configuramos UserAccount para aceitar tambem os atributos da associacao Permission.class UserAccount < ActiveRecord::Base attr_accessible :password, :username, :permissions_attributes validates_presence_of :username, :password, :person belongs_to :person has_many :permissions, inverse_of: :user_account accepts_nested_attributes_for :permissions, allow_destroy: trueendclass Permission < ActiveRecord::Base attr_accessible :restricted_area validates_presence_of :user_account, :restricted_area, :grants belongs_to :user_accountendFacamos alguns testes no console do Rails:> u = UserAccount.first UserAccount Load (0.2ms) SELECT "user_accounts".* FROM "user_accounts" LIMIT 1 => #<UserAccount id: 1, person_id: 1, username: "jsilva", password: "abc123", created_at: "2012-08-05 04:02:34", updated_at: "2012-08-05 04:02:34"> > u.update_attributes permissions_attributes: [ { restricted_area: :admin, grants: "Read-Write" }, { restricted_area: :backup, grants: "Read-Only" } ] (0.3ms) begin transaction Person Load (0.3ms) SELECT "people".* FROM "people" WHERE "people"."id" = 1 LIMIT 1 UserAccount Load (0.2ms) SELECT "user_accounts".* FROM "user_accounts" WHERE "user_accounts"."id" = 1 LIMIT 1 UserAccount Load (0.1ms) SELECT "user_accounts".* FROM "user_accounts" WHERE "user_accounts"."id" = 1 LIMIT 1 SQL (12.6ms) INSERT INTO "permissions" ("created_at", "restricted_area", "updated_at", "user_account_id") VALUES (?, ?, ?, ?) [["created_at", Sun, 05 Aug 2012 04:54:31 UTC +00:00], ["grants", "Read-Write"], ["restricted_area", :admin], ["updated_at", Sun, 05 Aug 2012 04:54:31 UTC +00:00], ["user_account_id", 1]] SQL (0.2ms) INSERT INTO "permissions" ("created_at", "restricted_area", "updated_at", "user_account_id") VALUES (?, ?, ?, ?) [["created_at", Sun, 05 Aug 2012 04:54:31 UTC +00:00], ["grants", "Read-Only"], ["restricted_area", :backup], ["updated_at", Sun, 05 Aug 2012 04:54:31 UTC +00:00], ["user_account_id", 1]] (58.7ms) commit transactionNo exemplo acima, permissoes foram adicionadas a uma conta existente de usuario. E possivel ir alem: todos os dados de UserAccount e Permission podem ser diretamente manipulados um nivel acima, numa chamada de Person:p = Person.create name: "Joao Oliveira", user_account_attributes: { username: "joliv", password: "secret", permissions_attributes: [ { restricted_area: "admin", grants: "Read-Write" }, { restricted_area: "backups", grants: "Read-Only" } ] } (0.1ms) begin transaction (0.0ms) commit transaction (0.1ms) begin transaction SQL (36.8ms) INSERT INTO "people" ("created_at", "name", "updated_at") VALUES (?, ?, ?) [["created_at", Sun, 05 Aug 2012 05:08:31 UTC +00:00], ["name", "Joao Oliveira"], ["updated_at", Sun, 05 Aug 2012 05:08:31 UTC +00:00]] SQL (0.6ms) INSERT INTO "user_accounts" ("created_at", "password", "person_id", "updated_at", "username") VALUES (?, ?, ?, ?, ?) [["created_at", Sun, 05 Aug 2012 05:08:31 UTC +00:00], ["password", "secret"], ["person_id", 3], ["updated_at", Sun, 05 Aug 2012 05:08:31 UTC +00:00], ["username", "joliv"]] SQL (0.4ms) INSERT INTO "permissions" ("created_at", "grants", "restricted_area", "updated_at", "user_account_id") VALUES (?, ?, ?, ?) [["created_at", Sun, 05 Aug 2012 05:08:31 UTC +00:00], ["grants",