{"record":{"id":"926583ec73570008","repo":"CanCanCommunity/cancancan","slug":"accessible-by-strategy-subquery-requires-active","errorCode":null,"errorMessage":"accessible_by_strategy = :subquery requires ActiveRecord 5 or newer","messagePattern":"accessible_by_strategy = :subquery requires ActiveRecord 5 or newer","errorType":"exception","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"lib/cancan/config.rb","lineNumber":71,"sourceCode":"    @accessible_by_strategy = default_accessible_by_strategy\n  end\n\n  def self.default_accessible_by_strategy\n    if does_not_support_subquery_strategy?\n      # see https://github.com/CanCanCommunity/cancancan/pull/655 for where this was added\n      # the `subquery` strategy (from https://github.com/CanCanCommunity/cancancan/pull/619\n      # only works in Rails 5 and higher\n      :left_join\n    else\n      :subquery\n    end\n  end\n\n  def self.accessible_by_strategy=(value)\n    validate_accessible_by_strategy!(value)\n\n    if value == :subquery && does_not_support_subquery_strategy?\n      raise ArgumentError, 'accessible_by_strategy = :subquery requires ActiveRecord 5 or newer'\n    end\n\n    @accessible_by_strategy = value\n  end\n\n  def self.with_accessible_by_strategy(value)\n    return yield if value == accessible_by_strategy\n\n    validate_accessible_by_strategy!(value)\n\n    begin\n      strategy_was = accessible_by_strategy\n      @accessible_by_strategy = value\n      yield\n    ensure\n      @accessible_by_strategy = strategy_was\n    end\n  end","sourceCodeStart":53,"sourceCodeEnd":89,"githubUrl":"https://github.com/CanCanCommunity/cancancan/blob/8c1bf153a3da7b2261d6fa4a5f84eb28e2feb828/lib/cancan/config.rb#L53-L89","documentation":"CanCan.accessible_by_strategy= (lib/cancan/config.rb:71) raises ArgumentError when you set :subquery but the loaded ActiveRecord adapter is older than 5.0 (or the ActiveRecord adapter is not loaded at all, e.g., CanCanCan loaded outside Rails). The subquery strategy relies on 'FROM (subquery) alias' joins introduced in Rails 5, so it cannot work on Rails 4.x.","triggerScenarios":"An initializer containing CanCan.accessible_by_strategy = :subquery in an app on Rails 4.2; a spec_helper that sets the strategy before the ActiveRecord adapter has been required; using CanCan::Config.with_accessible_by_strategy(:subquery) in a gem environment where ActiveRecord 4 is pinned.","commonSituations":"Copy-pasting the workaround initializer from a PR/blog post for the CanCanCan 619 duplicate-rows bug into a legacy Rails 4 app; CI running a different Rails version than production; loading cancancan in a Ruby-only (non-Rails) test suite.","solutions":["Stay on the default :left_join strategy when stuck on Rails < 5 (it is the default precisely for this reason).","Upgrade ActiveRecord/Rails to >= 5.0 to legitimately use :subquery.","Guard the initializer: only assign when CanCan.valid_accessible_by_strategies.include?(:subquery)."],"exampleFix":"# before (config/initializers/cancan.rb)\nCanCan.accessible_by_strategy = :subquery  # ArgumentError on Rails 4.2\n\n# after\nCanCan.accessible_by_strategy = :subquery if CanCan.valid_accessible_by_strategies.include?(:subquery)","handlingStrategy":"validation","validationCode":"strategy = :subquery\nraise ArgumentError, 'strategy unsupported on this ActiveRecord' unless CanCan.valid_accessible_by_strategies.include?(strategy)\nCanCan.accessible_by_strategy = strategy","typeGuard":null,"tryCatchPattern":"begin\n  CanCan.accessible_by_strategy = :subquery\nrescue ArgumentError => e\n  Rails.logger.warn(\"#{e.message}; staying on :left_join\")\nend","preventionTips":["Derive the strategy from CanCan.valid_accessible_by_strategies instead of hardcoding it.","Pin Rails >= 5 in the Gemfile when the app relies on the subquery strategy.","Run the initializer in CI on the lowest supported Rails version to catch version guards early."],"tags":["configuration","version-compatibility","active-record","cancancan","ruby"],"backgroundTag":"library-version-unsupported","analyzedSha":"8c1bf153a3da7b2261d6fa4a5f84eb28e2feb828","analyzedAt":"2026-08-21T20:05:55.000Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}