0

我有一个在 Rails 3.0 上编写的大项目。

该项目的最大模型是Questinary。它继承自 ActiveRecord::Base 作为所有典型模型。该模型的许多部分被提取到从 Questionary 继承的单独模型中。

我对此类模型中的范围有疑问。问题是 ActiveRecord 不了解范围,然后他们使用 Procs。带有 Procs 的范围进入 Questinary 模型本身可以正常工作。一切都很好,然后范围不包含 Proc。

错误信息是undefined method 'includes_values' for #<Proc:0x000000081cde98>

回溯:

activerecord (3.0.20) lib/active_record/relation/spawn_methods.rb:11:in `block in merge'
activerecord (3.0.20) lib/active_record/relation/spawn_methods.rb:10:in `each'
activerecord (3.0.20) lib/active_record/relation/spawn_methods.rb:10:in `merge'
activerecord (3.0.20) lib/active_record/named_scope.rb:32:in `scoped'
activerecord (3.0.20) lib/active_record/base.rb:449:in `rescue in order'
activerecord (3.0.20) lib/active_record/base.rb:447:in `order'
/home/sergey/.rvm/gems/ruby-1.9.3-p484/bundler/gems/active_admin-534e909fe7dc/lib/active_admin/resource_controller/collection.rb:51:in `sort_order'
/home/sergey/.rvm/gems/ruby-1.9.3-p484/bundler/gems/active_admin-534e909fe7dc/lib/active_admin/resource_controller/collection.rb:44:in `active_admin_collection'
/home/sergey/.rvm/gems/ruby-1.9.3-p484/bundler/gems/active_admin-534e909fe7dc/lib/active_admin/resource_controller/collection.rb:63:in `active_admin_collection'
/home/sergey/.rvm/gems/ruby-1.9.3-p484/bundler/gems/active_admin-534e909fe7dc/lib/active_admin/resource_controller/collection.rb:86:in `active_admin_collection'
/home/sergey/.rvm/gems/ruby-1.9.3-p484/bundler/gems/active_admin-534e909fe7dc/lib/active_admin/resource_controller/collection.rb:114:in `active_admin_collection'
/home/sergey/.rvm/gems/ruby-1.9.3-p484/bundler/gems/active_admin-534e909fe7dc/lib/active_admin/resource_controller/collection.rb:17:in `collection'
inherited_resources (1.2.2) lib/inherited_resources/actions.rb:7:in `index'
/home/sergey/.rvm/gems/ruby-1.9.3-p484/bundler/gems/active_admin-534e909fe7dc/lib/active_admin/resource_controller/actions.rb:11:in `index'
app/admin/questionary_scope_uni_groups.rb:216:in `index'

如您所见,从 ActiveAdmin 调用 ActiveRecord,因此问题可能与此有关。

Rails version: 3.0.20
ActiveAdmin version: 0.3.4

我知道旧的 gem 版本不好,但我不能更新它们,因为它会破坏应用程序。

大约一年前,这些东西运行良好,但后来服务器被拆解了。现在我需要重新激活它。

更新

范围示例:

default_scope proc { where(:status => "reviewed") }
4

1 回答 1

0

default_scope 有问题,写成:

default_scope proc { where(:status => "reviewed") }

我不可能只删除 proc (即default_scope where(:status => "reviewed")。因为在这种情况下 ActiveRecord 试图访问数据库,但我的应用程序中的数据库并非一直可用。

所以我需要一种懒惰的方式来使用 default_scope。

@andrey 的建议 ( scope :default_scope, lambda { where(:status => "reviewed") }) 不适合,因为涉及调用默认范围的所有代码的更改。

我尝试使用 block 而不是 proc (即default_scope { where(:status => "reviewed") }),但在 Rails 3.0 中我没有成功。

所以我将 default_scope 定义为这样的类方法:

def self.default_scope
  where(:status => "reviewed")
end

这完全符合我的需要。

于 2014-08-09T04:19:08.143 回答