问题
I am following the RailsTutorial and have been creating a Users model with appropriate attrs and validations. I've run into this error and cannot see where the problem is. The project is configured using Rails5, thus models inheriting from ApplicationRecord should be correct:
User.rb:
class User < ApplicationRecord
before_save { self.email = email.downcase }
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A([\w+\-].?)+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
has_secure_password
validates :password, length: { minimum: 6 }
end
Gemfile:
source 'https://rubygems.org'
ruby '2.3.0'
gem 'rails', '5.0.0.1'
gem 'bcrypt', '3.1.11'
gem 'faker', '1.6.3'
gem 'carrierwave', '0.11.2'
gem 'mini_magick', '4.5.1'
gem 'fog', '1.38.0'
gem 'will_paginate', '3.1.0'
gem 'bootstrap-will_paginate', '0.0.10'
gem 'bootstrap-sass', '3.3.6'
gem 'puma', '3.4.0'
gem 'sass-rails', '5.0.6'
gem 'uglifier', '3.0.0'
gem 'coffee-rails', '4.2.1'
gem 'jquery-rails', '4.1.1'
gem 'turbolinks', '5.0.1'
gem 'jbuilder', '2.4.1'
group :development, :test do
gem 'sqlite3', '1.3.11'
gem 'byebug', '9.0.0', platform: :mri
end
group :development do
gem 'web-console', '3.1.1'
gem 'listen', '3.0.8'
gem 'spring', '1.7.2'
gem 'spring-watcher-listen', '2.0.0'
end
group :test do
gem 'rails-controller-testing', '0.1.1'
gem 'minitest-reporters', '1.1.9'
gem 'guard', '2.13.0'
gem 'guard-minitest', '2.4.4'
end
group :production do
gem 'pg', '0.18.4'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
Gemfile.lock:
GEM
remote: https://rubygems.org/
specs:
CFPropertyList (2.3.2)
actioncable (5.0.0.1)
actionpack (= 5.0.0.1)
nio4r (~> 1.2)
websocket-driver (~> 0.6.1)
actionmailer (5.0.0.1)
actionpack (= 5.0.0.1)
actionview (= 5.0.0.1)
activejob (= 5.0.0.1)
mail (~> 2.5, >= 2.5.4)
rails-dom-testing (~> 2.0)
actionpack (5.0.0.1)
actionview (= 5.0.0.1)
activesupport (= 5.0.0.1)
rack (~> 2.0)
rack-test (~> 0.6.3)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.0.2)
actionview (5.0.0.1)
activesupport (= 5.0.0.1)
builder (~> 3.1)
erubis (~> 2.7.0)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.0.2)
activejob (5.0.0.1)
activesupport (= 5.0.0.1)
globalid (>= 0.3.6)
activemodel (5.0.0.1)
activesupport (= 5.0.0.1)
activerecord (5.0.0.1)
activemodel (= 5.0.0.1)
activesupport (= 5.0.0.1)
arel (~> 7.0)
activesupport (5.0.0.1)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (~> 0.7)
minitest (~> 5.1)
tzinfo (~> 1.1)
ansi (1.5.0)
arel (7.1.1)
autoprefixer-rails (6.4.1)
execjs
bcrypt (3.1.11)
bootstrap-sass (3.3.6)
autoprefixer-rails (>= 5.2.1)
sass (>= 3.3.4)
bootstrap-will_paginate (0.0.10)
will_paginate
builder (3.2.2)
byebug (9.0.0)
carrierwave (0.11.2)
activemodel (>= 3.2.0)
activesupport (>= 3.2.0)
json (>= 1.7)
mime-types (>= 1.16)
mimemagic (>= 0.3.0)
coderay (1.1.1)
coffee-rails (4.2.1)
coffee-script (>= 2.2.0)
railties (>= 4.0.0, < 5.2.x)
coffee-script (2.4.1)
coffee-script-source
execjs
coffee-script-source (1.10.0)
concurrent-ruby (1.0.2)
debug_inspector (0.0.2)
erubis (2.7.0)
excon (0.52.0)
execjs (2.7.0)
faker (1.6.3)
i18n (~> 0.5)
ffi (1.9.14)
fission (0.5.0)
CFPropertyList (~> 2.2)
fog (1.38.0)
fog-aliyun (>= 0.1.0)
fog-atmos
fog-aws (>= 0.6.0)
fog-brightbox (~> 0.4)
fog-cloudatcost (~> 0.1.0)
fog-core (~> 1.32)
fog-dynect (~> 0.0.2)
fog-ecloud (~> 0.1)
fog-google (<= 0.1.0)
fog-json
fog-local
fog-openstack
fog-powerdns (>= 0.1.1)
fog-profitbricks
fog-rackspace
fog-radosgw (>= 0.0.2)
fog-riakcs
fog-sakuracloud (>= 0.0.4)
fog-serverlove
fog-softlayer
fog-storm_on_demand
fog-terremark
fog-vmfusion
fog-voxel
fog-vsphere (>= 0.4.0)
fog-xenserver
fog-xml (~> 0.1.1)
ipaddress (~> 0.5)
fog-aliyun (0.1.0)
fog-core (~> 1.27)
fog-json (~> 1.0)
ipaddress (~> 0.8)
xml-simple (~> 1.1)
fog-atmos (0.1.0)
fog-core
fog-xml
fog-aws (0.11.0)
fog-core (~> 1.38)
fog-json (~> 1.0)
fog-xml (~> 0.1)
ipaddress (~> 0.8)
fog-brightbox (0.11.0)
fog-core (~> 1.22)
fog-json
inflecto (~> 0.0.2)
fog-cloudatcost (0.1.2)
fog-core (~> 1.36)
fog-json (~> 1.0)
fog-xml (~> 0.1)
ipaddress (~> 0.8)
fog-core (1.42.0)
builder
excon (~> 0.49)
formatador (~> 0.2)
fog-dynect (0.0.3)
fog-core
fog-json
fog-xml
fog-ecloud (0.3.0)
fog-core
fog-xml
fog-google (0.1.0)
fog-core
fog-json
fog-xml
fog-json (1.0.2)
fog-core (~> 1.0)
multi_json (~> 1.10)
fog-local (0.3.0)
fog-core (~> 1.27)
fog-openstack (0.1.12)
fog-core (>= 1.40)
fog-json (>= 1.0)
ipaddress (>= 0.8)
fog-powerdns (0.1.1)
fog-core (~> 1.27)
fog-json (~> 1.0)
fog-xml (~> 0.1)
fog-profitbricks (0.0.5)
fog-core
fog-xml
nokogiri
fog-rackspace (0.1.1)
fog-core (>= 1.35)
fog-json (>= 1.0)
fog-xml (>= 0.1)
ipaddress (>= 0.8)
fog-radosgw (0.0.5)
fog-core (>= 1.21.0)
fog-json
fog-xml (>= 0.0.1)
fog-riakcs (0.1.0)
fog-core
fog-json
fog-xml
fog-sakuracloud (1.7.5)
fog-core
fog-json
fog-serverlove (0.1.2)
fog-core
fog-json
fog-softlayer (1.1.4)
fog-core
fog-json
fog-storm_on_demand (0.1.1)
fog-core
fog-json
fog-terremark (0.1.0)
fog-core
fog-xml
fog-vmfusion (0.1.0)
fission
fog-core
fog-voxel (0.1.0)
fog-core
fog-xml
fog-vsphere (1.0.1)
fog-core
rbvmomi (~> 1.8.0)
fog-xenserver (0.2.3)
fog-core
fog-xml
fog-xml (0.1.2)
fog-core
nokogiri (~> 1.5, >= 1.5.11)
formatador (0.2.5)
globalid (0.3.7)
activesupport (>= 4.1.0)
guard (2.13.0)
formatador (>= 0.2.4)
listen (>= 2.7, <= 4.0)
lumberjack (~> 1.0)
nenv (~> 0.1)
notiffany (~> 0.0)
pry (>= 0.9.12)
shellany (~> 0.0)
thor (>= 0.18.1)
guard-compat (1.2.1)
guard-minitest (2.4.4)
guard-compat (~> 1.2)
minitest (>= 3.0)
i18n (0.7.0)
inflecto (0.0.2)
ipaddress (0.8.3)
jbuilder (2.4.1)
activesupport (>= 3.0.0, < 5.1)
multi_json (~> 1.2)
jquery-rails (4.1.1)
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
json (2.0.2)
listen (3.0.8)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
loofah (2.0.3)
nokogiri (>= 1.5.9)
lumberjack (1.0.10)
mail (2.6.4)
mime-types (>= 1.16, < 4)
method_source (0.8.2)
mime-types (3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2016.0521)
mimemagic (0.3.2)
mini_magick (4.5.1)
mini_portile2 (2.1.0)
minitest (5.9.0)
minitest-reporters (1.1.9)
ansi
builder
minitest (>= 5.0)
ruby-progressbar
multi_json (1.12.1)
nenv (0.3.0)
nio4r (1.2.1)
nokogiri (1.6.8)
mini_portile2 (~> 2.1.0)
pkg-config (~> 1.1.7)
notiffany (0.1.1)
nenv (~> 0.1)
shellany (~> 0.0)
pg (0.18.4)
pkg-config (1.1.7)
pry (0.10.4)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
puma (3.4.0)
rack (2.0.1)
rack-test (0.6.3)
rack (>= 1.0)
rails (5.0.0.1)
actioncable (= 5.0.0.1)
actionmailer (= 5.0.0.1)
actionpack (= 5.0.0.1)
actionview (= 5.0.0.1)
activejob (= 5.0.0.1)
activemodel (= 5.0.0.1)
activerecord (= 5.0.0.1)
activesupport (= 5.0.0.1)
bundler (>= 1.3.0, < 2.0)
railties (= 5.0.0.1)
sprockets-rails (>= 2.0.0)
rails-controller-testing (0.1.1)
actionpack (~> 5.x)
actionview (~> 5.x)
activesupport (~> 5.x)
rails-dom-testing (2.0.1)
activesupport (>= 4.2.0, < 6.0)
nokogiri (~> 1.6.0)
rails-html-sanitizer (1.0.3)
loofah (~> 2.0)
railties (5.0.0.1)
actionpack (= 5.0.0.1)
activesupport (= 5.0.0.1)
method_source
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
rake (11.2.2)
rb-fsevent (0.9.7)
rb-inotify (0.9.7)
ffi (>= 0.5.0)
rbvmomi (1.8.2)
builder
nokogiri (>= 1.4.1)
trollop
ruby-progressbar (1.8.1)
sass (3.4.22)
sass-rails (5.0.6)
railties (>= 4.0.0, < 6)
sass (~> 3.1)
sprockets (>= 2.8, < 4.0)
sprockets-rails (>= 2.0, < 4.0)
tilt (>= 1.1, < 3)
shellany (0.0.1)
slop (3.6.0)
spring (1.7.2)
spring-watcher-listen (2.0.0)
listen (>= 2.7, < 4.0)
spring (~> 1.2)
sprockets (3.7.0)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
sprockets-rails (3.2.0)
actionpack (>= 4.0)
activesupport (>= 4.0)
sprockets (>= 3.0.0)
sqlite3 (1.3.11)
thor (0.19.1)
thread_safe (0.3.5)
tilt (2.0.5)
trollop (2.1.2)
turbolinks (5.0.1)
turbolinks-source (~> 5)
turbolinks-source (5.0.0)
tzinfo (1.2.2)
thread_safe (~> 0.1)
uglifier (3.0.0)
execjs (>= 0.3.0, < 3)
web-console (3.1.1)
activemodel (>= 4.2)
debug_inspector
railties (>= 4.2)
websocket-driver (0.6.4)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.2)
will_paginate (3.1.0)
xml-simple (1.1.5)
PLATFORMS
ruby
DEPENDENCIES
bcrypt (= 3.1.11)
bootstrap-sass (= 3.3.6)
bootstrap-will_paginate (= 0.0.10)
byebug (= 9.0.0)
carrierwave (= 0.11.2)
coffee-rails (= 4.2.1)
faker (= 1.6.3)
fog (= 1.38.0)
guard (= 2.13.0)
guard-minitest (= 2.4.4)
jbuilder (= 2.4.1)
jquery-rails (= 4.1.1)
listen (= 3.0.8)
mini_magick (= 4.5.1)
minitest-reporters (= 1.1.9)
pg (= 0.18.4)
puma (= 3.4.0)
rails (= 5.0.0.1)
rails-controller-testing (= 0.1.1)
sass-rails (= 5.0.6)
spring (= 1.7.2)
spring-watcher-listen (= 2.0.0)
sqlite3 (= 1.3.11)
turbolinks (= 5.0.1)
tzinfo-data
uglifier (= 3.0.0)
web-console (= 3.1.1)
will_paginate (= 3.1.0)
RUBY VERSION
ruby 2.3.0p0
BUNDLED WITH
1.12.5
Backtrace from running heroku run console
:
... up, run.9075 (Free)
/app/app/models/user.rb:1:in `<top (required)>': uninitialized constant ApplicationRecord (NameError)
from /app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.0.1/lib/active_support/dependencies/interlock.rb:12:in `block in loading'
from /app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.0.1/lib/active_support/concurrency/share_lock.rb:117:in `exclusive'
from /app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.0.0.1/lib/active_support/dependencies/interlock.rb:11:in `loading'
from /app/vendor/bundle/ruby/2.3.0/gems/railties-5.0.0.1/lib/rails/engine.rb:476:in `block (2 levels) in eager_load!'
from /app/vendor/bundle/ruby/2.3.0/gems/railties-5.0.0.1/lib/rails/engine.rb:475:in `each'
from /app/vendor/bundle/ruby/2.3.0/gems/railties-5.0.0.1/lib/rails/engine.rb:475:in `block in eager_load!'
from /app/vendor/bundle/ruby/2.3.0/gems/railties-5.0.0.1/lib/rails/engine.rb:473:in `each'
from /app/vendor/bundle/ruby/2.3.0/gems/railties-5.0.0.1/lib/rails/engine.rb:473:in `eager_load!'
from /app/vendor/bundle/ruby/2.3.0/gems/railties-5.0.0.1/lib/rails/engine.rb:354:in `eager_load!'
from /app/vendor/bundle/ruby/2.3.0/gems/railties-5.0.0.1/lib/rails/application/finisher.rb:59:in `each'
from /app/vendor/bundle/ruby/2.3.0/gems/railties-5.0.0.1/lib/rails/application/finisher.rb:59:in `block in <module:Finisher>'
from /app/vendor/bundle/ruby/2.3.0/gems/railties-5.0.0.1/lib/rails/initializable.rb:30:in `instance_exec'
from /app/vendor/bundle/ruby/2.3.0/gems/railties-5.0.0.1/lib/rails/initializable.rb:30:in `run'
from /app/vendor/bundle/ruby/2.3.0/gems/railties-5.0.0.1/lib/rails/initializable.rb:55:in `block in run_initializers'
from /app/vendor/ruby-2.3.0/lib/ruby/2.3.0/tsort.rb:228:in `block in tsort_each'
from /app/vendor/ruby-2.3.0/lib/ruby/2.3.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
from /app/vendor/ruby-2.3.0/lib/ruby/2.3.0/tsort.rb:431:in `each_strongly_connected_component_from'
from /app/vendor/ruby-2.3.0/lib/ruby/2.3.0/tsort.rb:349:in `block in each_strongly_connected_component'
from /app/vendor/ruby-2.3.0/lib/ruby/2.3.0/tsort.rb:347:in `each'
from /app/vendor/ruby-2.3.0/lib/ruby/2.3.0/tsort.rb:347:in `call'
from /app/vendor/ruby-2.3.0/lib/ruby/2.3.0/tsort.rb:347:in `each_strongly_connected_component'
from /app/vendor/ruby-2.3.0/lib/ruby/2.3.0/tsort.rb:226:in `tsort_each'
from /app/vendor/ruby-2.3.0/lib/ruby/2.3.0/tsort.rb:205:in `tsort_each'
from /app/vendor/bundle/ruby/2.3.0/gems/railties-5.0.0.1/lib/rails/initializable.rb:54:in `run_initializers'
from /app/vendor/bundle/ruby/2.3.0/gems/railties-5.0.0.1/lib/rails/application.rb:352:in `initialize!'
from /app/config/environment.rb:5:in `<top (required)>'
from /app/vendor/bundle/ruby/2.3.0/gems/railties-5.0.0.1/lib/rails/application.rb:328:in `require'
from /app/vendor/bundle/ruby/2.3.0/gems/railties-5.0.0.1/lib/rails/application.rb:328:in `require_environment!'
from /app/vendor/bundle/ruby/2.3.0/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:157:in `require_application_and_environment!'
from /app/vendor/bundle/ruby/2.3.0/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:77:in `console'
from /app/vendor/bundle/ruby/2.3.0/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
from /app/vendor/bundle/ruby/2.3.0/gems/railties-5.0.0.1/lib/rails/commands.rb:18:in `<top (required)>'
from bin/rails:9:in `require'
from bin/rails:9:in `<main>'
boot.rb:
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
require 'bundler/setup' # Set up gems listed in the Gemfile.
console histroy, for what it's worth:
387 cd RailsTutorial/
388 cd sample_app/
389 subl .
390 git st
391 rails g migration add_password_digest_to_users password_digest:string
392 bundle exec rake db:migrate
393 rails console
394 git st
395 git add ,
396 git add .
397 git commit -am 'Make basic user model (inc secure passwords)"
398 '
399 git br
400 git co master
401 git push origin master
402 git push heroku
403 heroku run rake db:migrate
404 heroku run console
405 heroku run console --sandbox
406 heroku run rake db:migrate
407 heroku run console --sandbox
408 git st
409 git commit -am "Updating has_secure_password in User model"
410 '
411 git commit -am "Updating has_secure_password in User model"
412 '
413 git commit -am "Updating has_secure_password in User model"
414 git add .
415 git commit -am "Updating has_secure_password in User model"
416 git push origin master
417 git push heroku master
418 heroku run rake db:migrate
419 heroku console --sandbox
420 heroku console --sandbox
421 heroku run console --sandbox
422 git push heroku master
423 git st
424 heroku run console --sandbox
425 heroku run console
426 git st
427 git br
428 git commit -am 'bugging < ApplicationRecord error'
429 git push origin master
430 git push heroku master
431 heroku run rake db:migrate
432 herokue run console --sandbox
433 heroku run console --sandbox
434 rails s
435 git diff
436 git st
437 git br
438 git co modelling-users
439 git st
440 git co master
441 bundle exec guard
442 ruby /test/integration/site_layout_test.rb --name layout links
443 ruby /test/integration/site_layout_test.rb
444 ruby test/integration/site_layout_test.rb
445 rake test
446 rake test/integration/site_layout_test.rb
447 rake test/integration/site_layout_test.rb
448 ruby /test/integration/site_layout_test.rb --name layout links
449 ruby test/integration/site_layout_test.rb --name layout links
450 rake test TEST=test/integration/site_layout_test.rb
451 rake test TEST=test/integration/site_layout_test.rb name- layout links
452 ruby test/integration/site_layout_test.rb name- layout links
453 ruby test/integration/site_layout_test.rb --name layout links
454 rake test TEST=test/integration/site_layout_test.rb --name layout links
455 rake test TEST=test/integration/site_layout_test.rb --name layout links
456 ruby -Itest test/integration/site_layout_test.rb --name layout links
457 ruby -Itest test/integration/site_layout_test.rb --name layout_links
458 ruby -Itest test/integration/site_layout_test.rb --name layout_links
459 rake test TEST=test/integration/site_layout_test.rb
460 bundle exec guard
461 bundle exec guard
462 bundle exec guard
463 git st
464 git add .
465 git st
466 git st
467 git commit -am 'adding, migrating and testing user model with name and email attrs'
468 git st
469 git push origina master
470 git push heroku
471 git push heroku
472 bundle exec guard
473 bundle exec guard
474 bundle exec guard
475 rails s
476 rails s
477 rails s
478 pswl
479 psql
480 pwd
481 cd Code/
482 ls
483 cd RailsTutorial/sample_app/
484 rails test
485 rails test:models
486 bundle exec rake db:migrate
487 rails test:models
488 ls
489 git st
490 git diff
491 rails test:models
492 git st
493 git add .
494 git commit -am 'debugging ApplicationRecord'
495 git br
496 rails test
497 git push heroku master
498 heroku run rake db:migrate
499 heroku run console
500 pwd
501 cd Code/RailsTutorial/sample_app/
502 rails test
503 subl .
504 psql
505 rails test
506 rails test
507 git history
508 git hist
509 history
Just gone through whole directory file by file and have seen that my migrations directory and contents has completely dissapeared. V strange. The schema.rb is completely up to date. Command history above shows what's been done since I last saw the migrations directory and its correct migration file contents. Previous git commit was unfortunately before this whole model was generated thus have no previous commit with these migration contents
Any ideas of how to get me out of my amateur muddle? Thanks
回答1:
Create a new file called app/models/application_record.rb with the following contents:
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
回答2:
Turns out I had by mistake checked out from the brand where the migrations and models were created, back into master branch where these migrations and models were not present. Without those models present neither was the application_record.rb file thus the error of uninitialized constant ApplicationRecord
.
来源:https://stackoverflow.com/questions/41387706/uninitialized-constant-applicationrecord-error