我有这个模型
#post.rb
class Post < ActiveRecord::Base
belongs_to :user
after_initialize :create_token
attr_accessible :token
protected
def create_token
self.token = "#{Digest::MD5.hexdigest("#{self.id}")[0,4]}"
end
end
在rails c
Post.find(:all,:conditions => { :user_id => 11})
=> [#<Post id: 26, content: "<p><strong>Yao Ming</strong> (<a shape=\"rect\" title...", user_id: 11, created_at: "2011-07-12 15:08:30", updated_at: "2011-07-12 15:08:30", title: "Yao Ming", guid: "0010f6c3-040b-4e13-aa38-3a002e6f2022", contentHash: "\xB9\\\xCBK\xB0A>4\xC4~\xFC\"\xEA7\xA6y", token: "4e73">]
什么时候token: "4e73"
,但是当我尝试
Post.find(:all,:conditions => { :user_id => 11, :token => "4e73"})
=> []
我明白[]
了,为什么?
更多信息
Post.find(:all,:conditions => { :user_id => 11}).first.token.class
=> String
Post.find(:all,:conditions => { :user_id => 11}).first.token
=> "3769"