views:

136

answers:

1

In this video (10 sec) you can see that the nested attribute is only updated if it's parent model is changed.

Using rails 3.0.0.beta and full project is on github.

Summary of models and form:

class Project < ActiveRecord::Base
  has_many :tasks
  accepts_nested_attributes_for :tasks
end

class Task < ActiveRecord::Base
  belongs_to :project
  has_many :assignments
  accepts_nested_attributes_for :assignments
end

class Assignment < ActiveRecord::Base
  belongs_to :task
end


form_for(@project) do |f|

  Project: f.text_field :name

  f.fields_for :tasks do |task_form|
    Task: task_form.text_field :name

    task_form.fields_for :assignments do |assignment_form|
      Assignment: assignment_form.text_field :name
    end
  end

  f.submit
end
+1  A: 

EDIT2: The same code works perfectly in Rails 2.3.5. It seems to be a bug in beta version.

EDIT: I wrote a test in my app to simulate this. Here's the result.

  test "should update empresa" do
    addr = empresas(:ufba).address
    put_with users(:s_one), :update, :id => empresas(:ufba).to_param,
             :empresa => { :address_attributes => {:id => empresas(:ufba).address.to_param,
                                                   :city => "Feira de Santana"}}
    assert_not_nil assigns(:empresa)
    assert_not_same addr, assigns(:empresa).address, "Endereco nao foi atualizado"
    assert_redirected_to empresa_path(assigns(:empresa))
  end

It runs without errors.

nanda
I've been testing in the console, and only works for Rails 2.3.5, on Rails 3.0 the Assignment is not updated.
nanda
Cool, good to know. Thanks for the homework nanda!
chap
Opened a ticket on the rails tracker and added failing unit test to demonstrate:https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/4242-nested-child-only-updates-if-parent-changes
chap
I tried to find the code in Rails beta, still can't find where is the difference in the code that produces the error.
nanda