Forum: Ruby on Rails ActiveRecord can do conditonal update?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Jian Z. (Guest)
on 2007-07-28 00:55
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
Ball, Donald A Jr (Library) (Guest)
on 2007-07-28 01:15
(Received via mailing list)
> 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/Ba...

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

- donald
This topic is locked and can not be replied to.