How can i execute a sql sentence


#1

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


#2

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.


#3

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”


#4

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 :slight_smile:

Pat