Skip to content
This repository was archived by the owner on Oct 19, 2018. It is now read-only.
This repository was archived by the owner on Oct 19, 2018. It is now read-only.

Conflict with specific ActiveRecord scopes #15

Description

@adamcreekroad

There is some weird behavior happening with scopes with the names after and every.

For example if you have a model with a scope called after, it will work fine when called by itself Job.after. BUT if you put any other scope before it Job.active.after then it breaks with the error undefined method 'limit' for #<Thread:0x0000000003a45bc0>.

Tracking that down, it looks as though for some reason it is calling the Kernel#after defined in hyper-operation/delay_and_interval.

This can be replicated on the server in a rails console.

Activity

  1. adamcreekroad commented on Feb 5, 2018

    @adamcreekroad
    ContributorAuthor

    The solution (not the prettiest, but works)

    module Kernel
      def every(time, &block)
        if RUBY_ENGINE != 'opal' && defined?(ActiveRecord::Relation) && is_a?(ActiveRecord::Relation)
          super
        else
          Thread.new { loop { sleep time; block.call } }
        end
      end
    
      def after(time, &block)
        if RUBY_ENGINE != 'opal' && defined?(ActiveRecord::Relation) && is_a?(ActiveRecord::Relation)
          super
        else
          Thread.new { sleep time; block.call }
        end
      end
    end

    ActiveRecord's magic uses method missing to call the scope defined in the model on the ActiveRecord::Relation, but since Kernel defines those methods it never gets there. So there's really no nice way around it.

  2. added this to the Release 0.15 milestone on Feb 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions