New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NameError: uninitialized constant Mobility::ActiveRecord::Base #69

Closed
madis opened this Issue Aug 10, 2017 · 1 comment

Comments

Projects
None yet
2 participants
@madis
Contributor

madis commented Aug 10, 2017

When Mobility is loaded for ActiveRecord, it includes Mobility::ActiveRecord module

model_class.include(ActiveRecord) if model_class < ::ActiveRecord::Base

If the ActiveRecord::Base subclass that now includes Mobility did something like the following before:

class MyModel < ActiveRecord::Base
  def my_method
    ActiveRecord::Base.uncached do
      # some code
    end
  end
end

It will get an exception raised:

NameError:
  uninitialized constant Mobility::ActiveRecord::Base

It will get an exception because ActiveRecord is found on Mobility module first (the original ActiveRecord from rails is further down in the ancestors chain.

The fix on the user's side would be to qualify top level lookup with ::ActiveRecord but I don't know how happy the users of Mobility would be if they had to do it.

Another would be to rename the ActiveRecord module in Mobility, which would kind of diminish the descriptiveness.

Do you have other suggestions on how to approach this issue?

@shioyama shioyama added the question label Aug 10, 2017

@shioyama

This comment has been minimized.

Show comment
Hide comment
@shioyama

shioyama Aug 10, 2017

Owner

Wow, nice catch. Never even considered this problem.

There's one easy way to fix it, just extend Mobility instead of including it. Then it's not in the ancestors chain of the model, and internally that is what Mobility is doing anyway:

mobility/lib/mobility.rb

Lines 98 to 100 in 03c9cff

def included(model_class)
model_class.extend self
end

I think for this, maybe a note in the readme mentioning that both include and extend will work, but extend is preferred for this reason. I should probably change examples to use extend too.

Owner

shioyama commented Aug 10, 2017

Wow, nice catch. Never even considered this problem.

There's one easy way to fix it, just extend Mobility instead of including it. Then it's not in the ancestors chain of the model, and internally that is what Mobility is doing anyway:

mobility/lib/mobility.rb

Lines 98 to 100 in 03c9cff

def included(model_class)
model_class.extend self
end

I think for this, maybe a note in the readme mentioning that both include and extend will work, but extend is preferred for this reason. I should probably change examples to use extend too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment