<body>

Caiwangqin's blog

Focus on Web2.0, Business, Architecture, Agile, Technic and beyond…

Ruby on Rails中执行存储过程和指定SQL语句的方法

2006年9月10日 星期日

在使用RailsActiveRecord对数据库进行更新操作时,如果使用update(id,:field =>value) 方法更新数据,则此时rails并不是执行以下过程:


 1. Update table set field = value where id = id  而是实际执行了 2. Update table set field = value , field2 = value2 … where id = id 这意味着当数据库中这张表拥有50个字段的时候,Rails就分别更新了50个字段,只是将程序指定的field替换成了指定的value,其它没有指定的field一般不是我们所期望的执行更新的。 解决这个问题的方法是使用ActiveRecord::Baseconnection.execute方法,这个方法可以直接写更新数据库的sql语句,当然也可以执行SQL Server存储过程。使用以下语句就可以执行1中的数据库操作: Connection.execute(“update table set field = value where id = id”) 





标签:

posted by Caiwangqin, 上午1:24

<< 主页