"NameError: uninitialized constant" when fake exists in same file - ruby-on-rails

Say I have:
app/models/food/fruit.rb:
module Food
class Fruit
class_attribute :field_mapping
self.field_mapping = MyOrg::Trees::Details.field_mapping
# other things including:
end
end
In local/development mode, I can open a console and do/get this:
rails console
[1] pry(main)> Food::Fruit
NameError: uninitialized constant MyOrg::Trees::Details
MyOrg::Trees::Details will eventually be defined via a gem, but until then I want to fake it by adding the following to the top of the same file; why isn't this doing the trick?
module MyOrg
module Trees
class Details
def field_mapping
end
end
end
end
Note: when I paste the above into a local/development console, I can then successfully do this:
[2] pry(main)> MyOrg::Trees::Details.new.field_mapping
=> nil

When I added the above fake, I accidentally started trying to reference it to test what I was doing...
rails console
[1] pry(main)> MyOrg::Trees::Details
NameError: uninitialized constant MyOrg::Trees::Details
...but I should have called Food::Fruit as before, and I would have isolated the issue...
rails console
[1] pry(main)> Food::Fruit
NoMethodError: undefined method `field_mapping' for MyOrg::Trees::Details:Class
...ah, I need to make it a class method...
module MyOrg
module Trees
class Details
def self.field_mapping
end
end
end
end
...and then...
rails console
[1] pry(main)> Food::Fruit
NoMethodError: undefined method `values' for nil:NilClass
So, I appeased the undefined method error, and now have a new problem, which is that further down the original code there is actually another reference to field_mapping (field_mapping.values) which now has to be appeased...
module MyOrg
module Trees
class Details
def self.field_mapping
{}
end
end
end
end
...and now...
rails console
[1] pry(main)> Food::Fruit
=> Food::Fruit

Related

Uninitialized constant after upgrade rails 5.2 to 6.0

After upgrading my RoR app to 6.0, I'm getting this error:
NameError: uninitialized constant Sites::NetworkResolver::ExternalApi. This is my app/resolvers/sites/network_resolver.rb file:
module Sites
class NetworkResolver
ExternalApi::Graphql
...
end
end
and my app/services/external_api/graphql.rb
module ExternalApi
module Graphql
...
end
end
in the app/services folder, I have a settings file with the same name external_api.rb
module ExternalApi
...
end
rails zeitwerk:check
rails aborted!
NameError: uninitialized constant Sites::NetworkResolver::ExternalApi
If I put it at the top of app/resolvers/network_resolver.rb file
require 'external_api/graphql'
I need to update externalApi to externalAPI so that Api is uppercase
app/services/external_api/graphql.rb to define constant ExternalAPI::Graphql, but didn't
so code works but didn't want to update more than 100 files
By default, app/resolvers/network_resolver.rb should define NetworkResolver, rather than Sites:: NetworkResolver, maybe the path is mistaken?
You can force inflection for external_api to not use the acronym this way:
# config/initializers/zeitwerk.rb
Rails.autoloaders.main.inflector.inflect("external_api" => "ExternalApi")

Rails_admin undefined method `associations' for nil:NilClass

I have these models:
Class A
embeds_many :b
end
Class B
belongs_to :c
end
Class C
end
I'm working with rails_admin and mongoid. In admin, when I try to retrieve the list of C records when I'm creating an A instance I'm getting this error:
This only happens on production envirnment not in development
NoMethodError (undefined method `associations' for nil:NilClass):
/home/pablo/.rvm/gems/ruby-2.3.0#mh-backend/bundler/gems/rails_admin-355dc80f8a20/lib/rails_admin/adapters/mongoid/abstract_object.rb:10:in `initialize'
/home/pablo/.rvm/gems/ruby-2.3.0#mh-backend/bundler/gems/rails_admin-355dc80f8a20/lib/rails_admin/adapters/mongoid.rb:24:in `new'
/home/pablo/.rvm/gems/ruby-2.3.0#mh-backend/bundler/gems/rails_admin-355dc80f8a20/lib/rails_admin/adapters/mongoid.rb:24:in `get'
/home/pablo/.rvm/gems/ruby-2.3.0#mh-backend/bundler/gems/rails_admin-355dc80f8a20/app/controllers/rails_admin/main_controller.rb:138:in `get_association_scope_from_params'
Taking a look at rails_admin code we can see that piece of code in mongoid.rb file.
def get(id)
AbstractObject.new(model.find(id))
rescue => e
raise e if %w(
Mongoid::Errors::DocumentNotFound
Mongoid::Errors::InvalidFind
Moped::Errors::InvalidObjectId
BSON::InvalidObjectId
).exclude?(e.class.to_s)
end
If we pay attention at this code we can see that model.find(id) must produce a Mongoid::Errors::DocumentNotFound if document doesn't exists by default.
However, in mongoid you can avoid the raise of this error with raise_not_found_error: true in mongo config file, this produce the undefined method of nil class.
Tracking issue on github

Rails: NameError with lib module

In my device model I try to call a class method on a lib class, but on production I get a
NameError (uninitialized constant Device::Push):
It works on develoment, though.
But I don't call a Device::Push class. Instead that's what I'm doing in my Device model:
class Device < ActiveRecord::Base
...
after_update :send_notifications
...
def send_notifications
Push::NotificationFactory.send(device: self) if (self.push_token_changed? && ! self.push_token.nil?)
end
end
And in lib/push/notification_factory.rb the following class should be called:
module Push
class NotificationFactory
def self.send(device: )
n = Rpush::Apns::Notification.new
n.app = Rpush::Apns::App.find_by_name(Rails.configuration.x.app['push']['app_name'])
n.device_token = device.push_token
n.alert = 'Welcome to PrizeArena!'
n.save!
end
end
end
That's the error message I get:
2016-02-28T13:40:51.407542+00:00 app[web.1]: NameError (uninitialized constant Device::Push):
2016-02-28T13:40:51.407543+00:00 app[web.1]: app/models/device.rb:77:in `send_notifications'
2016-02-28T13:40:51.407544+00:00 app[web.1]: app/controllers/api/v1/users_controller.rb:44:in `update'
Would be great if somebody could point out where the NameError is coming from.
I figured out the problem why it worked on development but not on production.
I was adding lib to the auto_load path, but needed to add it to the eager_load_path.
You need to add
config.eager_load_paths << Rails.root.join('lib')
to application.rb
The problem was only happening on production, not on development, because on production Rails eager_loads the environment by default.
More information here:
http://blog.arkency.com/2014/11/dont-forget-about-eager-load-when-extending-autoload/
NameError (uninitialized constant Device::Push)
You need to use include to embed a module in a class.
class Device < ActiveRecord::Base
include Push
...
after_update :send_notifications
...
def send_notifications
Push::NotificationFactory.send(device: self) if (self.push_token_changed? && ! self.push_token.nil?)
end
end

undefined method `isolation_level' for ActiveRecord::Base:Class

Where does the call to the isolation_level come from?
My module fails at a.save!
module AppsHelpers
def self.create_app!
a = App.new
a.save!
a
end
end
The specific NoMethodError:
Failure/Error: #app = AppsHelpers::create_app!
NoMethodError:
undefined method `isolation_level' for ActiveRecord::Base:Class
What could possibly cause this failure?
System:
ruby 1.9.3p448
Rails 3.2.8
This was simple. I was calling a method defined in the Transactional Isolation gem: The stacktrace was not helpful because I was calling it from an installed gem and this was a missing dependency.

Forcing rails to autoload class

I have several small classes that are in a single file in /app/models, similar to:
# /app/models/little_class.rb
class LittleClass; ...do stuff; end;
class AnotherLittleClass; ...do stuff; end;
Rails only seems geared to autoload classes in files reflecting the class name. So referencing AnotherLittleClass outside of the file raises "unitialized constant" errors as below until LittleClass is referenced:
irb(main):001:0> AnotherLittleClass
NameError: uninitialized constant AnotherLittleClass
irb(main):02:0> LittleClass
=> LittleClass
irb(main):03:0> AnotherLittleClass
=> LittleClass2
It would be a pain and messy to split them into individual files. Is there a way to autoload these classes, so referencing AnotherLittleClass without LittleClass doesnt raise an error?
You could put them into a module and use them within this namespace SomeLittleClasses::LittleClass.do_something
# /app/models/some_little_classes.rb
module SomeLittleClasses
class LittleClass
def self.do_something
"Hello World!"
end
end
class AnotherLittleClass
def self.do_something
"Hello World!"
end
end
end
Try this trick:
1.9.2p312 :001 > AnotherLittleClass.new
# => NameError: uninitialized constant AnotherLittleClass
1.9.2p312 :002 > autoload :AnotherLittleClass, File.dirname(__FILE__) + "/app/models/little_class.rb"
# => nil
1.9.2p312 :003 > AnotherLittleClass.new
# => #<AnotherLittleClass:0xa687d24>
These are your choices, as I see it:
split your file up into one file per class, put them in a dir named according to the rails convention (SomeClass => some_class.rb) and in a startup file (say, create a file in config/initializers), call:
autoload_paths Rails.application.config.root + "/path/to/lib"
add something like this to a startup file:
%W[
Class1 Class2
Class3 Class4 Class4
].map(&:to_sym).each dp |klass|
autoload klass,Rails.application.config.root + "/path/to/lib/file"
end
This of course will have to be updated each time a new class is added to the file.
Move all of the classes into a module/class namespace and call autoload to add it as above
just load the whole file up-front in a startup file with require. Ask yourself: does the extra effort warrant delaying the load of this file?
The following file app/models/statistic.rb is given :
class Statistic
# some model code here
end
class UsersStatistic < Statistic; end
class CommentsStatistic < Statistic; end
class ConnectionsStatistic < Statistic; end
Create a file config/initializers/autoload_classes.rb and add the following code:
# Autoloading subclasses that are in the same file
# This is the normal way to load single classes
#
# autoload :UsersStatistic, 'statistic'
# autoload :CommentsStatistic, 'statistic'
# autoload :ConnectionsStatistic, 'statistic'
# This is the most dynamic way for production and development environment.
Statistic.subclasses.each do |klass|
autoload klass.to_s.to_sym, 'statistic'
end
# This does the same but loads all subclasses automatically.
# Useful only in production environment because every file-change
# needs a restart of the rails server.
#
# Statistic.subclasses

Resources