accepts_nested_atributes_for validating all children ? (in 2.3.5)

18 views
Skip to first unread message

jaambros

unread,
Apr 29, 2013, 4:19:31 PM4/29/13
to SD Ruby
Hi all, I've followed your list for sometime now and found always it
to be a great resource. I ask your help on this one after searching
for a solution all around to no avail.
I have an application where I used anaf in a model with children;
however as the number of children grew I noticed it was slowing up
quite a bit. After some analysis I found that when updating values for
one child anaf was doing validations on ALL chldren, which by then was
numbering in the thousands.

To illustrate my point I created this example:

class Language < ActiveRecord::Base
has_many :phrases
accepts_nested_attributes_for :phrases
end

class Phrase < ActiveRecord::Base
validates_presence_of :value
belongs_to :language
def before_validation
print "\nValidating: #{self.value}\n"
end
end

$ script/console
Loading development environment (Rails 2.3.5)
>> lang = Language.first
=> #<Language id: 1, name: "Italiano">
>> lang.phrases
=> [#<Phrase id: 7, value: "Buona notte", language_id: 1>,
#<Phrase id: 10, value: "Ciao", language_id: 1>,
#<Phrase id: 11, value: "Prego", language_id: 1>]

>> lang.phrases_attributes = [{:id => "7", :value => "Buon giorno"}]
=> [{:value=>"Buon giorno", :id=>"7"}]
>> lang.save

Validating: Buon giorno

Validating: Ciao

Validating: Prego

=> true

I expected in this example to see validation only on only one phrase
{id=>7}, but you can see it did it for all three.
I don't think this is expected behavior, right?
if so, can you help me find what I I'm missing?
After looking up at the code for anaf I suspect the issue may be in
autosave, but after a week trying to solve this I'm stumped.
can you help me?

Thanks in advance, Jose

Devin Wadsworth

unread,
Apr 29, 2013, 8:12:34 PM4/29/13
to sdr...@googlegroups.com
I don't have time to test it right now, but I think has_many :phrases, validate: false might be what you're looking for.

jaambros

unread,
Apr 29, 2013, 9:30:23 PM4/29/13
to SD Ruby
Devin, I already had tried
has_many :phrases, :validate=>false
and saw no difference

Jason King

unread,
Apr 30, 2013, 12:13:14 PM4/30/13
to SDRuby
Hi there Jose,

My first step would be to see if the problem still exists in the latest 2.3?  Ie. 2.3.18


--
--
SD Ruby mailing list
sdr...@googlegroups.com
http://groups.google.com/group/sdruby
---
You received this message because you are subscribed to the Google Groups "SD Ruby" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sdruby+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



jaambros

unread,
May 2, 2013, 11:30:19 PM5/2/13
to SD Ruby
Hi there Jason,

I updated to 2.3.18 and that solved the problem!

Thanks Jason and all for your help with this issue.

Jason King

unread,
May 5, 2013, 3:19:22 PM5/5/13
to SDRuby
Great news.
Reply all
Reply to author
Forward
0 new messages