<body><script type="text/javascript"> function setAttributeOnload(object, attribute, val) { if(window.addEventListener) { window.addEventListener('load', function(){ object[attribute] = val; }, false); } else { window.attachEvent('onload', function(){ object[attribute] = val; }); } } </script> <iframe src="http://www.blogger.com/navbar.g?targetBlogID=4684235500622716427&amp;blogName=Caiwangqin&#39;s+blog&amp;publishMode=PUBLISH_MODE_HOSTED&amp;navbarType=BLUE&amp;layoutType=CLASSIC&amp;searchRoot=http://blog.caiwangqin.com/search&amp;blogLocale=zh_CN&amp;v=1&amp;homepageUrl=http://blog.caiwangqin.com/&amp;vt=3393395200455623441" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" height="30px" width="100%" id="navbar-iframe" allowtransparency="true" title="Blogger 导航和搜索"></iframe> <div></div>

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 Jesse Cai, 上午1:24

0 Comments:

发表评论

订阅 帖子评论 [Atom]

<< 主页