2011-05-29 51 views
2

因为我用我试图像在最近的Rails以下3.1rc1应用:Rails 3.1rc1 to_param,from_param无效?

class Foo < ActiveRecord::Base 
    require 'digest/sha1' 

    belongs_to :user 
    after_create :set_uuid 

    def set_uuid 
    self.update_attribute :uuid, Digest::SHA1.hexdigest("#{APP_CONFIG[:salt]}_#{self.class}_#{self.id}")[0..14] 
    end 

    def self.from_param param 
    self.find_by_uuid! param 
    end 

    def to_param 
    self.uuid 
    end 

end 

Unforunately这似乎并没有产生任何影响了。当我请求:

http://localhost:3000/foos/3a2c4aa7d42adf3 

以下SQL发出:

SELECT "foos".* FROM "foos" WHERE "foos"."id" = ? LIMIT 1 [["id", "3a2c4aa7d42adf3"]] 

为了更好地实现我的目标,我可能也只是:

set_primary_key 'uuid' 

我知道,但是这不是我的方式d喜欢解决这个问题。

有没有人对我有过一些建议? Thx提前。

菲利克斯

回答

0

你确定你的控制器调用Foo.from_param(params[:id])?如果您使用的是脚手架,那么很可能是在正常使用Foo.find(params[:id])