# Inconsistent REST behavior

**URL:** <https://www.ruby-forum.com/t/inconsistent-rest-behavior/169328>\
**Category:** Rails\
**Created:** [June 2, 2009, 1:09pm UTC](https://www.ruby-forum.com/t/inconsistent-rest-behavior/169328 "2009-06-02T13:09:04Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![ncancelliere](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/9f8e36/32.png) [@ncancelliere](https://www.ruby-forum.com/u/ncancelliere)\
**Post date:** [June 2, 2009, 1:09pm UTC](https://www.ruby-forum.com/t/inconsistent-rest-behavior/169328/1 "2009-06-02T13:09:04Z")

</div>

Has anyone seen Rails save nested associations despite their being  
invalid?

If I hit the site from a different Rails app (using ActiveResource)  
and try to update the record so that the username is too short it  
fails (as I expect) and adds an errors attribute to the object that I  
can inspect. The original account (and associated user and email  
objects) is unaffected, as I’d expect.

Hitting the same exact URL with a raw HTTP/REST client and sending in  
XML:

\<?xml version="1.0" encoding="UTF-8"?\> 1 2010-01-05 soto removed\_email\_address@domain.invalid true false 

It gives me the expected 422 Unprocessible Entity error, which I  
expect. However it also saves user down as a null! That I do not  
expect.

My question is why does it correctly handle errors with ActiveResource  
(eg. doesn’t save anything down since their are validation problems)  
but with a raw PUT request it saves with a null user object  
(effectively killing the user).

# models

class User \< ActiveRecord::Base  
belongs\_to :account

validates\_length\_of :username, :minimum =\> 6

end

class Account \< ActiveRecord::Base

has\_one :user  
has\_one :email

has\_many :account\_entitlements  
has\_many :entitlements, :through =\> :account\_entitlements

accepts\_nested\_attributes\_for :user, :email

def user=(params)  
update\_attributes(:user\_attributes =\> params)  
end

def email=(params)  
update\_attributes(:email\_attributes =\> params)  
end

end

# controller

def update  
@account = Account.find(params[:id])

```
respond_to do |format|
  if @account.update_attributes(params[:account])
    flash[:notice] = 'Account was successfully updated.'
    format.html { redirect_to(@account) }
    format.xml { head :ok }
  else
    format.html { render :action => "edit" }
    format.xml { render :xml => @account.errors, :status

```

=\> :unprocessable\_entity }  
end  
end  
end
