繁体   English   中英

Rails 5.1:#的未定义方法“ apply_scope”

[英]Rails 5.1: undefined method `apply_scope' for #<ActiveRecord::Associations::AssociationScope

升级到Rails 5.1后出现此错误,无法找出问题所在:

undefined method `apply_scope' for #<ActiveRecord::Associations::AssociationScope

相关代码为:

def set_site
   @site = current_account.sites.active.find(params[:id])
   redirect_to sites_path if @site.nil?
   @site
end

模型:

class Site < ApplicationRecord
    belongs_to  :account
    scope :active, (-> { where(workflow_state: 'active') })
    # ....
end

全栈跟踪:

composite_primary_keys (10.0.1) lib/composite_primary_keys/associations/association_scope.rb:35:in `block in last_chain_scope'
composite_primary_keys (10.0.1) lib/composite_primary_keys/associations/association_scope.rb:33:in `each'
composite_primary_keys (10.0.1) lib/composite_primary_keys/associations/association_scope.rb:33:in `last_chain_scope'
activerecord (5.1.0) lib/active_record/associations/association_scope.rb:126:in `add_constraints'
/usr/local/bundle/bundler/gems/activemodel-associations-8dab1043fc77/lib/active_model/associations/association_scope_extension.rb:10:in `add_constraints'
activerecord (5.1.0) lib/active_record/associations/association_scope.rb:28:in `scope'
activerecord (5.1.0) lib/active_record/associations/association_scope.rb:5:in `scope'
activerecord (5.1.0) lib/active_record/associations/association.rb:97:in `association_scope'
activerecord (5.1.0) lib/active_record/associations/collection_proxy.rb:1162:in `association_scope'
activerecord (5.1.0) lib/active_record/associations/collection_proxy.rb:1131:in `method_missing'
app/controllers/sites_controller.rb:215:in `set_site'

看起来apply_scopeRails 5.2中的新方法:

def apply_scope(scope, table, key, value)
  if scope.table == table
    scope.where!(key => value)
  else
    scope.where!(table.name => { key => value })
  end
end

在以前的版本中,他们做到了:

scope = scope.where(table.name => { key => value })

因此,我假设您已经安装了Rails 5.x,但是调用了Rails 5.2方法。 如果为true,请检查您的gemfile并运行bundle show rails以查看您使用的Ruby和Ruby on Rails版本。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM