Rails 5: Uninitialized constant in view/controller but not console - ruby-on-rails

In my Rails 5 app I have a module in app/lib
module LibClass
CONSTANT_NAME = ‘somevalue’
end
Then in a model I reference the module:
class SomeModel < ApplicationRecord
def lib_class_constant
LibClass::CONSTANT_NAME
end
end
Everything works as expected when I call lib_class_constant on an instance of a SomeModel in console
But if I do the same in a view:
<%= some_model_instance.lib_class_constant %>
I get an error along the lines of:
uninitialized constant SomeModel::LibClass
If I reference the module directly in the view:
<%= LibClass::CONSTANT_NAME %>
I get an error along the lines of:
uninitialized constant ActionView::CompiledTemplates::LibClass
What am I missing here?

Have you tried the line include LibClass right after class SomeModel < ApplicationRecord ?
Otherwise have you checked that models and files have the right names? i.e. sometimes you rename a model without renaming the file accordingly or vice versa...

Related

uninitialized constant name error

I have model as follows
app/models/views/def_usage.rb
class Abc
class Def < ActiveRecord::Base
self.table_name = 'vSomeview'
end
end
I am trying to create a factory girl for this
spec/factories/views/def_usage.rb
FactoryGirl.define do
factory :def_usage, class: Abc::DefUsage do
......
end
end
I am getting error uninitialized constant Abc::DefUsage (NameError)
I tried changing class: Views::Abc::DefUsage or Views::DefUsage but no luck. i am getting that error when i am trying to do rails console. why i am getting that error?
Your path needs to match your module/class hierarchy.
If you want your class to be in app/models/views/def.rb, then your class needs to be Views::Def.
If you want your class to be Abc::Def, your path needs to be app/models/abc/def.rb.
If you want your class name to be DefUsage, your file name needs to be def_usage.rb.
You can't use arbitrary paths and class names. They need to match if you want Rails to automatically load constants for you.

New model in Spree extension: uninitialized constant Spree::MyClass

I'm trying to create a new model in spree extension. I generated a model and it is in /spree_extension/app/models/my_class.rb:
module Spree
class MyClass < Spree::Base
belongs_to :product
end
end
But when I start my application, there is no Spree::MyClass, I get this error:
NameError: uninitialized constant Spree::MyClass
I tried moving my_class.rb to "spree" directory, but nothing helps.
Most probably, you need to put your class into:
/spree_extension/app/models/spree/my_class.rb
As rails is always expecting to find classes inside file with the same name, inside folder that has the module name.
The problem was in fact I created a table my_class.
Since I renamed it to spree_my_class, it works.

Use a model concern

Ripping my hair out on this one:
app/models/concerns/soft_delete.rb:
module SoftDelete
extend ActiveSupport::Concern
module ClassMethods
def testing_a_class
pp "Ima class method"
end
end
def testing_an_instance
pp "Ima instance method"
end
end
class User < ActiveRecord::Base
include SoftDelete
end
app/models/user.rb:
class User < ActiveRecord::Base
testing_a_class
end
Now, in the rails console:
x = User.first # I expect "Ima class method" to be printed to the screen
NameError: undefined local variable or method `testing_a_class' for User(no database connection):Class
I don't know where you saw this idea of including a module in the same file where it's defined, but you must not do it (in rails), because of how rails works (auto-lazy-loading).
Rails doesn't load all classes on startup. Instead when you reference a class that doesn't yet exist, rails attempts to guess where it might be located and loads it from there.
x = User.first
Before this line, constant User does not exist (assuming it wasn't referenced before). When trying to resolve this name, rails will look for file user.rb in each of autoload_paths (google it up). It will find one at app/models/user.rb. Next time you reference User, it will just use this constant and will not look it up in the filesystem.
# app/models/user.rb:
class User < ActiveRecord::Base
testing_a_class
end
The definition that was found contains only an invocation of some unknown method (hence the error). Code in your concern file was not loaded and will never be loaded. To fix this, include the concern in the model file.
# app/models/user.rb:
class User < ActiveRecord::Base
include SoftDelete
testing_a_class
end
Now it should work.

Unitialized Constant for custom rails class

I am trying to create an instance of class in a rails model
/app/models/employee.rb
class Employee < ActiveRecord::Base
def self.import(file)
preferences = ::MotionlessAgitator::EmployeeAvailability.new
...
end
end
except it does exist in:
/app/models/motionlessagitator/employeeavailability.rb
module MotionlessAgitator
class EmployeeAvailability
def initialize(csv_name = nil)
I am being given this error:
NameError (uninitialized constant MotionlessAgitator):
app/models/employee.rb:5:in `import'
app/controllers/employees_controller.rb:65:in `import'
Have tried calling with/without the "::" and from within the controller. I'm still fairly new at this and not exactly sure how the load paths work though
You are missing underscores in directory and file names. In order to get your class loaded automatically, you should have it in app/models/motionless_agitator/employee_availability.rb file.

rails models

i have a model named test.rb and when i use #tests=Test.new in my controller i get the following error. Can someone temme how can i resolve this?
"undefined method `new' for Test:Module"
Looks like test is already the name of a module called Test if would seem that you have naming conflict. Try placing your own model in a module ie
module MyModule
class Test < ActiveRecord::Base
end
end
and then calling it like so
#test = MyModule::Test.new

Resources