# Problem with model validation with ajax

**URL:** <https://www.ruby-forum.com/t/problem-with-model-validation-with-ajax/185556>\
**Category:** Rails\
**Created:** [March 22, 2010, 6:12pm UTC](https://www.ruby-forum.com/t/problem-with-model-validation-with-ajax/185556 "2010-03-22T18:12:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![inprogs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/8491ac/32.png) [@inprogs](https://www.ruby-forum.com/u/inprogs)\
**Post date:** [March 22, 2010, 6:12pm UTC](https://www.ruby-forum.com/t/problem-with-model-validation-with-ajax/185556/1 "2010-03-22T18:12:54Z")

</div>

I have got a problem with :on =\> create validation

validates\_uniqueness\_of :nip, :regon, on =\> :create

Doesn’t work properly when I call it via xhr

In my log

Processing AdminController#doajaxing\_for\_updateuser (for 127.0.0.1 at  
2010-03-22 17:37:13) [PUT]  
Parameters:  
{“account”=\>{“company\_attributes”=\>{“nip”=\>“634-119-69-92”}},  
“named”=\>“company|nip”, “locale”=\>“pl”}

Processing AdminController#doajaxing\_for\_createuser (for 127.0.0.1 at  
2010-03-22 17:31:33) [POST]  
Parameters:  
{“account”=\>{“company\_attributes”=\>{“nip”=\>“634-119-69-95”}},  
“named”=\>“company|nip”, “locale”=\>“pl”}

It looks fine, Put and Post is given by my jquery request…

But still validation is always invoking validates\_uniqueness\_of …  
why /.??

I check validation by:

@model.valid?

and later bringing messages using something like this

@model.errors[current\_given]

regards

---

<div class="post-metadata">

**Author:** ![inprogs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/8491ac/32.png) [@inprogs](https://www.ruby-forum.com/u/inprogs)\
**Post date:** [March 22, 2010, 6:14pm UTC](https://www.ruby-forum.com/t/problem-with-model-validation-with-ajax/185556/2 "2010-03-22T18:14:39Z")

</div>

On Mar 22, 4:59Â pm, Piotr [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) MÄ…sior  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have got a problem with :on =\> create validation
> 
> validates\_uniqueness\_of :nip, :regon, on =\> :create

The :on there doesn’t refer to the controller action - it refers to  
whether the save is an update to an existing record or not.

Fred

---

<div class="post-metadata">

**Author:** ![inprogs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/8491ac/32.png) [@inprogs](https://www.ruby-forum.com/u/inprogs)\
**Post date:** [March 22, 2010, 7:47pm UTC](https://www.ruby-forum.com/t/problem-with-model-validation-with-ajax/185556/3 "2010-03-22T19:47:45Z")

</div>

So painful, I was wondering is there any dry\_save or something like  
this ?

---

<div class="post-metadata">

**Author:** ![inprogs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/8491ac/32.png) [@inprogs](https://www.ruby-forum.com/u/inprogs)\
**Post date:** [March 22, 2010, 7:57pm UTC](https://www.ruby-forum.com/t/problem-with-model-validation-with-ajax/185556/4 "2010-03-22T19:57:35Z")

</div>

Piotr MÄ…sior wrote:

> So painful, I was wondering is there any dry\_save or something like  
> this ?

How do you populate your @model?

Stephan

---

<div class="post-metadata">

**Author:** ![inprogs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/8491ac/32.png) [@inprogs](https://www.ruby-forum.com/u/inprogs)\
**Post date:** [March 22, 2010, 8:57pm UTC](https://www.ruby-forum.com/t/problem-with-model-validation-with-ajax/185556/5 "2010-03-22T20:57:29Z")

</div>

Model is passed by @model variable with nested attributes so it is  
like:

@model = model.new  
@model.build\_another

at form it is looped through like

- form\_for @account, :url =\> { :action =\> @given\_action , :controller  
=\> ‘admin’} do |f|
- f.fields\_for :company do |o|  
%label{:for =\> “company|name”}=  
Company.human\_attribute\_name(:name)  
= o.text\_field :name, :named =\> “company|name”  
- o.fields\_for :city do |x|  
%label{:for =\> “company|city|name”}=  
City.human\_attribute\_name(:name)  
= x.text\_field :name, :named =\> “company|city|name”

jquery is responsible for communication and requesting my particular  
methods so it calls appropriate model basing on something like  
params[:model], doajaxing method knows how to deal with nested  
attributes

so it request from controller some action like  
doajaxing\_for\_createmodel and it looks like:

def doajaxing\_for\_createmodel  
doajaxing(“account”)  
end

and doajaxing itself looks like

def doajaxing(object\_name)  
if request.xhr?  
sended = params[object\_name.to\_sym]  
object\_name = object\_name.camelize  
@process = object\_name.constantize  
@process = @process.new(sended)  
@process.valid?  
##some other processing  
if @process.errors[current\_given]  
#process message and field  
#some @response is given in json  
render :json =\> @response  
end  
end

that it work… I wonder if I can use instead of @process.valid?  
something else

---

<div class="post-metadata">

**Author:** ![inprogs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/8491ac/32.png) [@inprogs](https://www.ruby-forum.com/u/inprogs)\
**Post date:** [March 22, 2010, 9:21pm UTC](https://www.ruby-forum.com/t/problem-with-model-validation-with-ajax/185556/6 "2010-03-22T21:21:40Z")

</div>

Piotr MÄ…sior wrote:

> Model is passed by @model variable with nested attributes so it is  
> like:
> 
> @model = model.new

> @model.build\_another
> 
> at form it is looped through like
> 
> - form\_for @account, :url =\> { :action =\> @given\_action , :controller  
> =\> ‘admin’} do |f|
> - f.fields\_for :company do |o|  
> %label{:for =\> “company|name”}=  
> Company.human\_attribute\_name(:name)  
> = o.text\_field :name, :named =\> “company|name”  
> - o.fields\_for :city do |x|  
> %label{:for =\> “company|city|name”}=  
> City.human\_attribute\_name(:name)  
> = x.text\_field :name, :named =\> “company|city|name”
> 
> jquery is responsible for communication and requesting my particular  
> methods so it calls appropriate model basing on something like  
> params[:model], doajaxing method knows how to deal with nested  
> attributes
> 
> so it request from controller some action like  
> doajaxing\_for\_createmodel and it looks like:
> 
> def doajaxing\_for\_createmodel  
> doajaxing(“account”)  
> end
> 
> and doajaxing itself looks like
> 
> def doajaxing(object\_name)  
> if request.xhr?  
> sended = params[object\_name.to\_sym]  
> object\_name = object\_name.camelize  
> @process = object\_name.constantize  
> @process = @process.new(sended)

Because you have “@process.new”, the “on\_create” validations will be  
triggered.

If you are updating an existing record, use  
@process = @process.find(…model-id…)  
where model-id is usually taken from “params” (params[:id]).  
Then assign the new values to @process.

Then the “on\_create” validations will not be triggered.

> ```
> @process.valid?
> 
> ```
> 
> ##some other processing  
> if @process.errors[current\_given]  
> #process message and field  
> #some @response is given in json  
> render :json =\> @response  
> end  
> end
> 
> that it work… I wonder if I can use instead of @process.valid?  
> something else

Hope that helps. I think your “doajaxing” method needs to know about the  
two cases, create/update.

Stephan

---

<div class="post-metadata">

**Author:** ![inprogs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/8491ac/32.png) [@inprogs](https://www.ruby-forum.com/u/inprogs)\
**Post date:** [March 22, 2010, 9:38pm UTC](https://www.ruby-forum.com/t/problem-with-model-validation-with-ajax/185556/7 "2010-03-22T21:38:28Z")

</div>

I just realized it too… so bad for me 🙂

Ok, I thought there would be another more convenient way but as I can  
now see there is not

Regards, thanks everyone
