far so good.
There are a few variants on this. First you can use
ActiveRecord::Base.connection if you don’t care to nominate a
particular model. The point however is that if your models lived in
different databases you’d need to pick the right one. The connection
adapters have several methods that execute raw sql, of which execute
is the most primitive. There’s also select_all (and a few similiar
ones), update, and delete, which differ because they return stuff.
As far as an update goes you can also do MyModel.update_all
“crd_status=‘N’”, “rcrd_status=‘Y’ AND foreign_id=‘abcdefg’”
MyModel.connection.execute(some_arbitrarily_hazardous_sql)
is the most primitive. There’s also select_all (and a few similiar
ones), update, and delete, which differ because they return stuff.
As far as an update goes you can also do MyModel.update_all
“crd_status=‘N’”, “rcrd_status=‘Y’ AND foreign_id=‘abcdefg’”
Fred
UPDATE mydatabase.myrecords SET rcrd_status=‘N’ WHERE rcrd_status=‘Y’
AND
foreign_id=‘abcdefg’;
Furthermore, if rcrd_status is actually defined as a :boolean, then
I’d replace ‘N’ with false and ‘Y’ with true and let the database
adapter sort out how the boolean is implemented. You also don’t have
to worry about the right quoting for the foreign_id with this syntax
(exactly the same as in a :conditions value on a find).
Afaik, update_all doesn’t support placeholders. Did something change or
did I just miss it somehow initially? I hacked together a helper that
did, but update_all would only take conditions as a string, not an
array.
Ah. I guess I just didn’t see that one staring me in the face
[“rcrd_status = :disabled”, {:disabled => ‘N’}],
[“rcrd_status = :enabled AND foreign_id = :keyval”,
{:enabled => ‘Y’, :keyval => userID}])
Ah. I guess I just didn’t see that one staring me in the face
(update_all, delete_all).
(exactly the same as in a :conditions value on a find).
Speaking of “exactly the same”… I wanted to use this format, but it
doesn’t work. Unless I’m just not doing it correctly, I guess Rails
doesn’t support this same format that is supported with find?