Forum: Ruby on Rails how can i execute a sql sentence

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.
j. smith (Guest)
on 2006-12-29 03:51
hi everybody

i want to execute a sql sentence like  "UPDATE employes set status = 1
WHERE sex = 1"

if any know how to do it...
please tell me

thanks
Steve K. (Guest)
on 2006-12-29 05:28
I believe that .find_by_sql will allow you to do pure sql queries
bypassing the ActiveRecord engine.  Downside is you lose the bonuses it
gives you to prevent SQL injection attacks and single/double quoting
issues.
Pat M. (Guest)
on 2006-12-29 05:34
(Received via mailing list)
On 12/28/06, j. smith <removed_email_address@domain.invalid> wrote:
>
> hi everybody
>
> i want to execute a sql sentence like  "UPDATE employes set status = 1
> WHERE sex = 1"

Employee.update_all "status=1", "sex=1"
Pat M. (Guest)
on 2006-12-29 05:50
(Received via mailing list)
On 12/28/06, Steve K. <removed_email_address@domain.invalid> wrote:
>
>
> I believe that .find_by_sql will allow you to do pure sql queries
> bypassing the ActiveRecord engine.  Downside is you lose the bonuses it
> gives you to prevent SQL injection attacks and single/double quoting
> issues.

If you're doing updates, it's better to just do

ActiveRecord::Base.connection.execute "MY SQL HERE"

you can use a model class instead of AR::Base if you want there, it
doesn't matter.  Well actually it does, if your model class is using a
different connection then the default.  But it doesn't matter the vast
majority of the time :)

Pat
This topic is locked and can not be replied to.