Why am I getting this error?
https://gist.github.com/770219
Your LegalDocument model may be defined in a filename that is not correct. The file must be located in any subdirectory of app and be called legal_document.rb. The name is really important, as this is how Rails does autoloading.
Related
I have a query object in Rails 5.2.2
app/queries/car_query.rb
class CarQuery
attr_reader :relation
# code
end
when I reference it in the console, I get
Error
NameError (uninitialized constant CarQuery)
Since the folder/file is in the app directory, I thought it automatically gets loaded. Am I incorrect? If so, why would my form objects autoload and not these? How do I fix this?
Try running bin/spring stop in your console then restarting the app.
If that doesn't work add update you application.rb with
Spring.watch "app/queries/**"
Make sure that the name of the file corresponds to the class name, or else rails will have trouble loading it. That is to say, make sure the file name is car_query.rb, if CarQuery is the name of the class.
CarQuery.rb goes against rails naming conventions and will mess with autoloading.
I recently decided to change the name of a model. So I dropped the DB calling rails db:drop.
Using sublime text editor I did a find & replace for the model name, and I renamed all relevant directories to be in line with convention; including the migration file etc.
I deleted the schema file.
I called rails db:create to create the DB: no errors
I called rails db:migrate to create the tables and schema: no errors.
When I try to call a method on the model class... ('Set')
> Set.all
NoMethodError: undefined method `all' for Set:Class
from (pry):4:in `<main>'
For a test, I ran rails generate model Set and got the following:
Running via Spring preloader in process 2042
invoke active_record
The name 'Set' is either already used in your application or reserved by Ruby on Rails. Please choose an alternative and run this generator again.
So here it seems the model does exist. But it doesn't exist in console.
Why is my model nil
It's not. It's unclear where you drew this conclusion from.
NoMethodError: undefined method `all' for Set:Class
Name of your model (Set) is the same as class from the standard lib. Due to how rails' autoloading works, name Set is resolved to the stdlib class, not your model. Rename your model with a non-existing name (just as the warning suggests).
I thought everything in the App folder autoloads. Why would I be getting an (uninitialized constant) error?
app/form_models/user.rb
module Wizard
module User
end
end
I have been following these instructions.
https://medium.com/#nicolasblanco/developing-a-wizard-or-multi-steps-forms-in-rails-d2f3b7c692ce
However, keep getting error and the blog states:
" Remember that the Rails autoloading feature will load every Ruby class inside the app folder"
According to Rails auto-loader conventions this should be located in some path ending in wizard/user.rb but it's not.
One place to put it is app/models/concerns/wizard/user.rb where it can be loaded.
New to ROR working through a tutorial attempting to generate a default policy file for my application with the pundit:install.
$ rails g pundit:install generates the following error:
.rvm/gems/ruby-2.2.0/gems/devise-3.4.1/lib/devise/models.rb:88:in `const_get': wrong constant name Admin? (NameError)
I also tried captilizing pundit with the following command:
$ rails g Pundit:install , but it generates the same error:
The given error message doesn't yet point to Pundit as being the problem:
.rvm/gems/ruby-2.2.0/gems/devise-3.4.1/lib/devise/models.rb:88:
in `const_get': wrong constant name Admin? (NameError)
Is there a longer stack trace for the error message? If yes, please add it to the question.
Also search your app's files for Admin? (with a capital 'A'). See that Admin? is mentioned in the error message wrong constant name Admin?.
(At a guess, somewhere in the app, Admin? is written where it should not be. Perhaps it needs to be removed or replaced with small 'a': admin?. Or replaced with Admin without the question mark.)
I have run a migration and am getting a 500 error saying seen_by_hw does not exist. Could this be due to the fact I have used an uppercase characters when creating the ruby model. eg
seen_by_HW. Or should it be seen_by_hw
thanks
If you created the model file/class yourself then yes, this is probably the issue.
The filename should be: seen_by_hw.rb and in that the class name will be SeenByHw.
If you use the generators that comes with Rails then it would have done this for you, type rails generate in your app directory to see more info on them.