# ActiveRecord can do conditonal update?

**URL:** <https://www.ruby-forum.com/t/activerecord-can-do-conditonal-update/110599>\
**Category:** Rails\
**Created:** [July 27, 2007, 10:55pm UTC](https://www.ruby-forum.com/t/activerecord-can-do-conditonal-update/110599 "2007-07-27T22:55:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jzhang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/df788c/32.png) [@jzhang](https://www.ruby-forum.com/u/jzhang)\
**Post date:** [July 27, 2007, 10:55pm UTC](https://www.ruby-forum.com/t/activerecord-can-do-conditonal-update/110599/1 "2007-07-27T22:55:52Z")

</div>

ActiveRecord update method can support condition (SQL where clause)?

My rails application always query data by some unique key then update  
this data (if no data, create one). It is two SQL statement. Want to  
improve the performance, combine it into one SQL update statement.

if ActiveRecord not support, can i have some way to customize  
ActiveRecord ?

Thanks a lot  
–Jian

---

<div class="post-metadata">

**Author:** ![jzhang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/df788c/32.png) [@jzhang](https://www.ruby-forum.com/u/jzhang)\
**Post date:** [July 27, 2007, 11:15pm UTC](https://www.ruby-forum.com/t/activerecord-can-do-conditonal-update/110599/2 "2007-07-27T23:15:59Z")

</div>

> ActiveRecord update method can support condition (SQL where clause)?
> 
> My rails application always query data by some unique key  
> then update this data (if no data, create one). It is two SQL  
> statement. Want to improve the performance, combine it into  
> one SQL update statement.
> 
> if ActiveRecord not support, can i have some way to customize  
> ActiveRecord ?

[http://api.rubyonrails.com/classes/ActiveRecord/Base.html#M000999](http://api.rubyonrails.com/classes/ActiveRecord/Base.html#M000999)

you’ll need to check the update count to see if the object doesn’t  
already exist, of course.

- donald
