|
網誌存檔
熱門網志
|
|
|
1 class Blog < ActiveRecord::Base 2 # belongs_to :blog_category,:foreign_key => "category_id" 3 # 重寫了ActiveRecoed的兩個method來實現 4 def update_attribute(name, value) 5 update_attributes(name => value) 6 end 7 8 def update_attributes(new_attributes) 9 return if new_attributes.nil? 10 attributes = new_attributes.dup 11 attributes.stringify_keys! 12 self.attributes = attributes 13 update(attributes) 14 end 15 16 private 17 def update(attrs = nil) 18 connection.update( 19 "UPDATE #{self.class.table_name} " + 20 "SET #{quoted_comma_pair_list(connection, attributes_with_quotes(false, attrs))} " + 21 "WHERE #{self.class.primary_key} = #{quote_value(id)}", 22 "#{self.class.name} Update" 23 ) 24 25 return true 26 end 27 28 def attributes_with_quotes(include_primary_key = true, attrs = nil) 29 (attrs || attributes).inject({}) do |quoted, (name, value)| 30 if column = column_for_attribute(name) 31 quoted[name] = quote_value(value, column) unless !include_primary_key && column.primary 32 end 33 quoted 34 end 35 end 36 end 使用方法跟原來的方法一樣。 27 @blog = Blog.find(params["id"]) 問題就解決了。 |
-------------------------------------------------
| 上一篇:Rails MD5 SHA1 加密用戶密碼 | 下一篇:How to do friendly date formatting in Ruby |

