some child errors do not appear in parent using accepts_nested_attributes_for (2.3.18)

5 views
Skip to first unread message

jaambros

unread,
May 7, 2013, 5:40:15 PM5/7/13
to SD Ruby
Hi all,
Again I ask for your help with this issue after searching for a while
for a solution to no avail.
This simplified example illustrates the problem.

class User < ActiveRecord::Base
has_many :projects
accepts_nested_attributes_for :projects
end

class Project < ActiveRecord::Base
belongs_to :user
validates_presence_of :spec_name

before_validation :show
def show
puts "validating #{self.name}"
end
end

$ ./script/console
Loading development environment (Rails 2.3.18)
>> u1 = User.first
=> #<User id: 1, name: "Mary", created_at: "2013-05-07 19:51:56",
updated_at: "2013-05-07 19:51:56">
>> u1.projects_attributes = [{:spec_name=>'', :name=>'n1'}, {:spec_name=>'', :name=>'n2'}]
=> [{:spec_name=>"", :name=>"n1"}, {:spec_name=>"", :name=>"n2"}]
>> u1.valid?
validating n1
validating n2
=> false
>> pp u1.errors
#<ActiveRecord::Errors:0x103454a80
@base=
#<User id: 1, name: "Mary", created_at: "2013-05-07 19:51:56",
updated_at: "2013-05-07 19:51:56">,
@errors=
{["projects.spec_name",
[#<ActiveRecord::Error:0x103452938
@attribute=:spec_name,
@base=
#<Project id: nil, user_id: 1, name: "n1", spec_name: "",
created_at: nil, updated_at: nil>,
@message=:blank,
@options={:default=>nil},
@type=:blank>]]=>nil}>
=> nil

As you can see, validation is done on both projects. However, when
looking at the errors it only shows one of them. I expected to see
both. The same thing occurs when updating instead of creating.

- Am I missing something?
- If not, can you suggest a workaround?

Reply all
Reply to author
Forward
0 new messages