# Parent model running validations on all existing children

**URL:** <https://www.ruby-forum.com/t/parent-model-running-validations-on-all-existing-children/190658>\
**Category:** Rails\
**Created:** [June 24, 2010, 6:02pm UTC](https://www.ruby-forum.com/t/parent-model-running-validations-on-all-existing-children/190658 "2010-06-24T18:02:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubenrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9a140/32.png) [@rubenrails](https://www.ruby-forum.com/u/rubenrails)\
**Post date:** [June 24, 2010, 6:02pm UTC](https://www.ruby-forum.com/t/parent-model-running-validations-on-all-existing-children/190658/1 "2010-06-24T18:02:02Z")

</div>

In my application, a Language has many Phrases. Here is my code:

#language model  
has\_many :phrases  
accepts\_nested\_attributes\_for :phrases

#phrase model  
validates\_presence\_of :value  
belongs\_to :language

#languages controller  
def edit  
@language = Language.find(params[:id])

# Let’s say we only want to update some of the phrases (e.g. by

category, date, etc)  
@phrases = @language.phrases.all(:limit =\> 3)  
end

def update  
@language = Language.find(params[:id])  
if @language.update\_attributes(params[:language])  
flash[:notice] = “Successfully updated language.”  
redirect\_to edit\_language\_path(@language)  
else  
render :action =\> ‘edit’  
end  
end

# project edit view

\<% form\_for @language do |f| %\>  
\<%= f.error\_messages %\>

\<%= f.label :name %\>  
 \<%= f.text\_field :name %\>

 \<% f.fields\_for :phrases, @phrases do |phrases\_form| %\> 

\<%= phrases\_form.label :value %\> \<%= phrases\_form.text\_field :value %\>

 \<% end %\> 

\<%= f.submit %\>

\<% end %\>

When I submit the form, if at least one of the phrases being saved is  
an existing record, every single phrase corresponding to @language  
will be validated, instead of running validations only in the three  
phrases being updated. That problem isn’t present when all of the  
phrases being saved are new records.

---

<div class="post-metadata">

**Author:** ![rubenrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9a140/32.png) [@rubenrails](https://www.ruby-forum.com/u/rubenrails)\
**Post date:** [June 24, 2010, 7:10pm UTC](https://www.ruby-forum.com/t/parent-model-running-validations-on-all-existing-children/190658/2 "2010-06-24T19:10:56Z")

</div>

I was expecting Rails to only validate the phrase that is being updated,  
here you can see it’s validating all of the existing phrases:

$ 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

My models, again, now with the added “before\_validation” callback:

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  
logger.debug “\nValidating: #{self.value}\n”  
end  
end

Validating all phrases is a real problem in my app, as I have tens of  
thousands; Is there a way to avoid this? Is this normal behavior for  
Rails?

Ruben Ascencio wrote:

> In my application, a Language has many Phrases. Here is my code:
> 
> #language model  
> has\_many :phrases  
> accepts\_nested\_attributes\_for :phrases
> 
> #phrase model  
> validates\_presence\_of :value  
> belongs\_to :language
> 
> #languages controller  
> def edit  
> @language = Language.find(params[:id])
> 
> # Let’s say we only want to update some of the phrases (e.g. by
> 
> category, date, etc)  
> @phrases = @language.phrases.all(:limit =\> 3)  
> end
> 
> def update  
> @language = Language.find(params[:id])  
> if @language.update\_attributes(params[:language])  
> flash[:notice] = “Successfully updated language.”  
> redirect\_to edit\_language\_path(@language)  
> else  
> render :action =\> ‘edit’  
> end  
> end
> 
> # project edit view
> 
> \<% form\_for @language do |f| %\>  
> \<%= f.error\_messages %\>
> 
> \<%= f.label :name %\>  
> \<%= f.text\_field :name %\>
> 
> \<% f.fields\_for :phrases, @phrases do |phrases\_form| %\> 
> 
> \<%= phrases\_form.label :value %\> \<%= phrases\_form.text\_field :value %\>
> 
> \<% end %\> 
> 
> \<%= f.submit %\>
> 
> \<% end %\>
> 
> When I submit the form, if at least one of the phrases being saved is  
> an existing record, every single phrase corresponding to @language  
> will be validated, instead of running validations only in the three  
> phrases being updated. That problem isn’t present when all of the  
> phrases being saved are new records.
