新手问题 ActiveRecord 得 find 有缓存嘛?

kaka · December 14, 2012 · Last by cantin replied at December 14, 2012 · 3465 hits

难道 find 有缓存

有缓存

update: 我查查先

def find_one(id)
  id = id.id if ActiveRecord::Base === id

  if IdentityMap.enabled? && where_values.blank? &&
    limit_value.blank? && order_values.blank? &&
    includes_values.blank? && preload_values.blank? &&
    readonly_value.nil? && joins_values.blank? &&
    !@klass.locking_enabled? &&
    record = IdentityMap.get(@klass, id)
    return record
  end

  column = columns_hash[primary_key]

  substitute = connection.substitute_at(column, @bind_values.length)
  relation = where(table[primary_key].eq(substitute))
  relation.bind_values = [[column, id]]
  record = relation.first

  unless record
    conditions = arel.where_sql
    conditions = " [#{conditions}]" if conditions
    raise RecordNotFound, "Couldn't find #{@klass.name} with #{primary_key}=#{id}#{conditions}"
  end

  record
end

如果 IdentityMap 没开应该没缓存啊。

装了什么插件吗?

#3 楼 @Rei https://github.com/lshgo/mongo_organizational 可能是转换模型 database 的问题吧!但是 where 怎么会没有查到呢!

#3 楼 @Rei 他用的明显是 Mongodb.........

应该是 where(_id: id) 吧?

#7 楼 @cantin 我是用 mongoid

#4 楼 @kaka 有部分查询逻辑被拆分到 origin 这个 gem 了

mongoid 也有 identity_map_enabled 这个参数,有没有打开?

7# +1,mongo 主键是_id 不是 id

#9 楼 @Rei 没有开启 identity_map_enabled

#10 楼 @jjym

Industry.where(:id => "503f06b7e13823df08000004").first
  MOPED: 127.0.0.1:27017 QUERY        database=neza_public_development collection=industries selector={"$query"=>{"_id"=>"503f06b7e13823df08000004"}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 fields=nil (0.6375ms)
 => #<Industry _id: 503f06b7e13823df08000004, _type: nil, created_at: 2012-08-30 06:22:47 UTC, updated_at: 2012-08-30 06:22:47 UTC, name: "**", alias_name: "***"> 

我没有用 origin 这个 gem

我不知道了,一个个 gem 去掉看看。

#12 楼 @kaka 我又 out 了...

#8 楼 @kaka mongoid 中的 id 是 Moped::BSON::ObjectId,也就是个 object,你要用 current_user.id 来查询的话,要

User.where(_id: current_user.id)

另外 key 为 id,value 就是 String.

You need to Sign in before reply, if you don't have an account, please Sign up first.