# ActiveRecord: save\_only\_valid\_attributes?

**URL:** <https://www.ruby-forum.com/t/activerecord-save-only-valid-attributes/148580>\
**Category:** Rails\
**Created:** [September 15, 2008, 9:38am UTC](https://www.ruby-forum.com/t/activerecord-save-only-valid-attributes/148580 "2008-09-15T09:38:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![brady8](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecccb3/32.png) [@brady8](https://www.ruby-forum.com/u/brady8)\
**Post date:** [September 15, 2008, 9:38am UTC](https://www.ruby-forum.com/t/activerecord-save-only-valid-attributes/148580/1 "2008-09-15T09:38:57Z")

</div>

Hello,

I’m looking for a variation on the “save” method, that will only save  
attributes that do not have errors attached to them.

So a model can be updated without being valid overall, and this will  
still prevent saving invalid data to the database.

I’ve been looking through the Rails code for a while now, and I’ve  
been unable to find anything that might work. Does anyone have an  
ideas for how I can accomplish this?

Thanks for your time,  
Brady

---

<div class="post-metadata">

**Author:** ![brady8](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecccb3/32.png) [@brady8](https://www.ruby-forum.com/u/brady8)\
**Post date:** [September 15, 2008, 12:30pm UTC](https://www.ruby-forum.com/t/activerecord-save-only-valid-attributes/148580/2 "2008-09-15T12:30:53Z")

</div>

You could solve it by doing something like this?

def save\_valid\_attributes  
self.valid?  
self.attributes.each do |k,v|  
self.update\_attribute(k.to\_sym, v) unless  
self.errors.on(k.to\_sym)  
end  
end

Rob L.

---

<div class="post-metadata">

**Author:** ![brady8](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecccb3/32.png) [@brady8](https://www.ruby-forum.com/u/brady8)\
**Post date:** [September 15, 2008, 1:39pm UTC](https://www.ruby-forum.com/t/activerecord-save-only-valid-attributes/148580/3 "2008-09-15T13:39:45Z")

</div>

something like

def save\_valid\_attributes  
valid?  
update\_atrtibutes attributes.inject({}){|k, v, m| m[k] = v unless  
errors\_on(k.to\_sym); m}  
end

would save you multiple hits on the db for single attributes. check out  
Enumerable#inject for more information  
[http://www.ruby-doc.org/core/classes/Enumerable.html#M003160](http://www.ruby-doc.org/core/classes/Enumerable.html#M003160)

HTH

RSL

---

<div class="post-metadata">

**Author:** ![brady8](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecccb3/32.png) [@brady8](https://www.ruby-forum.com/u/brady8)\
**Post date:** [September 15, 2008, 2:22pm UTC](https://www.ruby-forum.com/t/activerecord-save-only-valid-attributes/148580/4 "2008-09-15T14:22:11Z")

</div>

Russell N. wrote:

> def save\_valid\_attributes  
> valid?  
> update\_atrtibutes attributes.inject({}){|k, v, m| m[k] = v unless  
> errors\_on(k.to\_sym); m}  
> end

And note this violates the GUI principle of least surprise.

If a customer were changing credit card and billing address at the same  
time,  
they don’t need the billing address to stick in the database when the CC  
bounces.

I’m aware the actual problem may be different here…

–  
Phlip

---

<div class="post-metadata">

**Author:** ![brady8](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecccb3/32.png) [@brady8](https://www.ruby-forum.com/u/brady8)\
**Post date:** [September 15, 2008, 4:31pm UTC](https://www.ruby-forum.com/t/activerecord-save-only-valid-attributes/148580/5 "2008-09-15T16:31:12Z")

</div>

Thanks, that inject code will work nicely!  
And yes, I’m a good like interface designer, due to the nature of what  
I’m working on there is lots of partial updates to data before the  
model instance is full and valid, and the user is notified that  
partial updates are saved.

Thanks again, I appreciate it.

-Brady

---

<div class="post-metadata">

**Author:** ![brady8](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecccb3/32.png) [@brady8](https://www.ruby-forum.com/u/brady8)\
**Post date:** [September 15, 2008, 2:35pm UTC](https://www.ruby-forum.com/t/activerecord-save-only-valid-attributes/148580/6 "2008-09-15T14:35:54Z")

</div>

it still violates it in the original code. it just violates it with less  
db  
hits.

RSL
