Tag: 延迟加载

Rails 3拒绝急于加载

我在Rails 3.0.7中工作。 我有一些多对多和一些一对多的关联,无法加载。 我的协会是: Person has_many :friends Person has_many :locations through=> :location_histories Location belongs_to :location_hour Location_hour has_many :locations 在我的控制器中,我有以下内容: @people = Person.includes([[:locations=>:location_hour],:friends]).where(“(location_histories.current = true) AND (people.name LIKE ? OR friends.first_name LIKE ? OR friends.last_name LIKE ? OR (friends.first_name LIKE ? AND friends.last_name LIKE ?))”).limit(10).all 然后在我看来,我有: locations.current是一个定义为: scope :current, lambda { where(“location_histories.current = ?”, true) } […]

如何在rails 2.3.3中有条件地分配ActionController :: Base.session

我有一个rails应用程序,在config / initializers / session_store.rb文件中包含以下内容: ActionController::Base.session = { :key => ‘_app_session’, :secret => ‘a really long string here’, :expire_after => 2.minutes } ActionController::Base.session_store = :active_record_store 因此,在正常操作期间,我们看到在数据库中为每个会话创建的ActiveRecord对象。 问题是我们并不总是希望为请求创建会话 – 我们希望能够关闭自动请求的会话创建。 我们在数据库中看到数千个会话记录,每个自动请求一个。 在rails 2.3.3之前,可能存在以下情况: class ApplicationController < ActionController::Base session :off … end 但现在在rails 2.3.3中“session:off”已被弃用,因为会话现在是延迟加载的 – 如果你不使用它们,它们就不会被创建。 问题似乎是会话对象始终在sessionstore.rb文件中分配,因此始终创建。 如果我从配置文件中删除会话分配块,则在自动请求后不会显示会话记录。 我的问题是,如何将配置分配移出session_store.rb并进入ApplicationController类(或其他地方),只有在请求不是自动化的情况下才能有条件地分配会话? 我担心在执行控制器处理程序之前可能需要会话配置数据。 我在哪里可以分配会话密钥值? 在此先感谢您的帮助。

为什么Rails模型关联结果不是自然的ActiveRecord :: Relations?

我正在使用Rails 3.2.0 假设我有: class Comment < ActiveRecord::Base has_many :articles end c1 = Comment.last 然后 c1.articles.class # => Array c1.articles.where(‘id NOT IN (999999)’).class # => ActiveRecord::Relation 为什么关联的结果不是 ActiveRecord::Relation的类型? 它显然是/ 在某个时候 : c1.articles.to_orig # undefined method `to_orig’ for # c1.articles.class # => Array 某些评估作用于ActiveRecord :: Relation对象,但检查该类会给出不同的类型。 特别是,当使用merge来连接多个查询时,这会破坏构建延迟加载的查询。

`ClassName.constants`在Rails应用程序中返回空数组

我正在开发一个Rails 3应用程序,我的lib文件夹中有一个类的层次结构,例如: lib ├── assets ├── tasks │ └── import.rake └── importer ├── base.rb └── source ├── facebook.rb ├── google.rb └── twitter.rb 我已经更新了config/application.rb以包含这一行: config.autoload_paths += %W(#{config.root}/lib) 然后在Importer::Base里面,我有一个实例方法试图加载Provider模块中的所有类,例如: Importer::Source.constants.each do |class_name| Importer::Source.const_get(class_name).process end lib/importer/base的三个类具有类似于以下的类层次结构: module Importer module Source class Facebook # … end end end 当我调用此方法时, Importer::Source.constants最终返回一个空数组。 如果我直接通过名称引用它们,那么这些类似乎是延迟加载的,但它们在constants调用中是不可访问的。 我怎样才能解决这个问题?