简体   繁体   中英

Cannot load my model in console rails

I tried to view what is on my Tasks table and all of the sudden I am now getting a uninitialized constant error. I cannot think why this is happening. The page is working fine. By the way, I do have a task.rb in my model.

server@server:~/www/railsapp/$ rails c production
Running via Spring preloader in process 27137
Loading production environment (Rails 5.1.1)
irb(main):001:0> ActiveRecord::Base.connection.tables
   (0.3ms)  SET NAMES utf8,  @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'),  @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483
=> ["ar_internal_metadata", "schema_migrations", "tasks", "users"]
irb(main):002:0> Task.all
NameError: uninitialized constant Task
    from (irb):2
irb(main):003:0> 

I finally found out the solution for the problem. It was Spring causing the issue. I provided the link to the StackOverflow question down below.

I had to run in the terminal:

spring stop

Now when I start my console it can read my models.

The link I found for solution is here

Create a file task.rb in app/models folder:

app/model/task.rb

class Task < ActiveRecord::Base

end

That mean for some reason rails did not load file where your model(class name) live.

If your "page" work I suppose that you change something in application but not restart your production(production work on old code). @Alejandro Montilla ask about changes but you didn answer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM