嗨,我有一个应用模型,我尝试在 rails 控制台中查找_by_hashid!
ruby -v
ruby 1.9.3p125 (2012-02-16 revision 34643) [x86_64-darwin11.1.0]
gem 'rails', '4.0.0.rc1'
我的模型:
class App < ActiveRecord::Base
include ActiveModel::Dirty
include Tokenable
has_many :categories, dependent: :destroy
before_save :notify_categories
def notify_categories
self.categories.map!{|c| c.update_results(self.data)}
end
def to_param
self.hashid
end
end
在控制台中
App.find_by_hashid "e2eed536bb2"
App Load (0.4ms) SELECT "apps".* FROM "apps" WHERE "apps"."hashid" = 'e2eed536bb2' LIMIT 1 => nil
App.where(:hashid =>"e2eed536bb2")
App Load (0.3ms) SELECT "apps".* FROM "apps" WHERE "apps"."hashid" = 'e2eed536bb2' => #<ActiveRecord::Relation []>
应用程序哈希
App.first.hashid
App Load (0.2ms) SELECT "apps".* FROM "apps" ORDER BY "apps"."id" ASC LIMIT 1
=> "e2eed536bb2"
这返回真
App.first.hashid == "e2eed536bb2"
App Load (0.4ms) SELECT "apps".* FROM "apps" ORDER BY "apps"."id" ASC LIMIT 1 => true
甚至试试这个。我只有一个“应用程序”
App.find_by_hashid(App.first.hashid)
App Load (0.3ms) SELECT "apps".* FROM "apps" ORDER BY "apps"."id" ASC LIMIT 1
App Load (0.3ms) SELECT "apps".* FROM "apps" WHERE "apps"."hashid" = 'e2eed536bb2' LIMIT 1 => nil