# Update\_attributes and save!

**URL:** <https://www.ruby-forum.com/t/update-attributes-and-save/97887>\
**Category:** Rails\
**Created:** [July 11, 2006, 2:24am UTC](https://www.ruby-forum.com/t/update-attributes-and-save/97887 "2006-07-11T02:24:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 11, 2006, 2:24am UTC](https://www.ruby-forum.com/t/update-attributes-and-save/97887/1 "2006-07-11T02:24:48Z")

</div>

I notice that ActiveRecord::Base.update\_attributes() calls save().

What if I want to get an exception if the save fails? Is there a way to  
get the update\_attributes() function plus the save! function?

Thanks,  
Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 11, 2006, 2:36am UTC](https://www.ruby-forum.com/t/update-attributes-and-save/97887/2 "2006-07-11T02:36:28Z")

</div>

> What if I want to get an exception if the save fails? Is there a way to  
> get the update\_attributes() function plus the save! function?

Have you tried to raise an error based on the return value of the save?

unless Something.update\_attributes() raise “error message”

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 11, 2006, 2:39am UTC](https://www.ruby-forum.com/t/update-attributes-and-save/97887/3 "2006-07-11T02:39:39Z")

</div>

Wes G. wrote on 11.07.2006 02:24:

> I notice that ActiveRecord::Base.update\_attributes() calls save().
> 
> What if I want to get an exception if the save fails? Is there a way to  
> get the update\_attributes() function plus the save! function?

You could do

raise Exception unless update\_attributes()

or

attributes=()  
save!()

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 11, 2006, 3:16am UTC](https://www.ruby-forum.com/t/update-attributes-and-save/97887/4 "2006-07-11T03:16:35Z")

</div>

Or just define your own update\_attributes! method in environment.rb:

class ActiveRecord::Base  
def update\_attributes!(attributes)  
self.attributes = attributes  
save!  
end  
end

-Jonathan.
