uninitialized constant ActiveRecord - ruby-on-rails

2.0.0-p247 :006 > load './app/models/user.rb'
NameError: uninitialized constant ActiveRecord
from /home/action/iAuth/app/models/user.rb:1:in `<top (required)>'
from (irb):6:in `load'
from (irb):6
from /home/action/.rvm/rubies/ruby-2.0.0-p247/bin/irb:13:in `<main>'
Below is my User Model.
class User < ActiveRecord::Base
end
When i try to load my user.rb in my irb, i get the above error.

start your irb session with
rails console
and not:
irb
rails console would load your rails environment and your model for you, so you can do things like:
User.all or User.new without loading the class as it has been preloaded by rails console already

there's another case.
if you are using mongoid ( mongo db adapter) , and you have to make sure in config/application.rb, you must require activerecord:
require "active_model/railtie"

Related

undefined method `default' for ActionMailer

I'm having this weird error with Rails 5 action mailer.
# app/mailers/application_mailer.rb
class ApplicationMailer < ActionMailer::Base
default from: "from#example.com"
layout 'mailer'
end
irb
~/workspace/rails5 > rails c ruby-2.4.1#rails5
Running via Spring preloader in process 45498
Loading development environment (Rails 5.1.2)
2.4.1 :001 > ApplicationMailer
NameError: uninitialized constant Mail::TestMailer
from app/mailers/application_mailer.rb:1:in `<top (required)>'
from (irb):1
2.4.1 :002 > ApplicationMailer
NoMethodError: undefined method `default' for ApplicationMailer:Class
from app/mailers/application_mailer.rb:3:in `<class:ApplicationMailer>'
from app/mailers/application_mailer.rb:1:in `<top (required)>'
from (irb):2
2.4.1 :003 >
This is the default Scaffold of rails 5.
Two really weird things happen the first is that it always tells me the uninitialized constant then trows default is not a method right after.
Just wondering how to fix it or if I should downgrade to Rail 5.0
The solution turned out to be that if you have a model named mail the ActionMailer will grab that one first instead of the rails class. If this happens then it fails the first time but the second time will load the rails class and try and smash them together. Allowing the submodule to work but treating the main class as a monkey path.

Why is irb returning NameError: uninitialized constant for all objects in the database?

I am half way through building a Q&A app and I have successfully seeded my database and can see that objects have been created both in my browser and in the server logs and previously in irb. Now when I went to try to inspect an object in irb, for every object I give it, irb returns "NameError: uninitialized constant". For example:
2.0.0-p481 :001 > user = User.find(1)
NameError: uninitialized constant User
from (irb):1
from /home/vagrant/.rvm/rubies/ruby-2.0.0-p481/bin/irb:12:in `<main>'
2.0.0-p481 :002 > question = Question.find(1)
NameError: uninitialized constant Question
from (irb):2
from /home/vagrant/.rvm/rubies/ruby-2.0.0-p481/bin/irb:12:in `<main>'
2.0.0-p481 :003 > user = User.find
NameError: uninitialized constant User
from (irb):3
from /home/vagrant/.rvm/rubies/ruby-2.0.0-p481/bin/irb:12:in `<main>'
2.0.0-p481 :004 > user = User.new
NameError: uninitialized constant User
from (irb):4
from /home/vagrant/.rvm/rubies/ruby-2.0.0-p481/bin/irb:12:in `<main>'
I'm new to programming, so if anyone could explain why this might be happening it would be much appreciated!
It looks like you're running irb when you want rails console (or rails c for short).
Because you need to use rails console, not irb.
rails console
(I assume you are building a Rails application)

Sidekiq Install - uninitialized constant ActiveRecord::Base (NameError)

I am trying to integrate Sidekiq with my Rails Mongoid app. I have been following the steps as outlined Here
I also have redis & mongodb running locally. However after I run bundle then when I try to start my server I get this error....
c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/sidekiq-2.17.7/lib/sidekiq/rails.rb:4:in `hook_rails!': uninitialized constant ActiveRecord::Base (NameError)
from c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/sidekiq-2.17.7/lib/sidekiq/rails.rb:16:in `block in <class:Rails>'
from c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/initializable.rb:30:in `instance_exec'
from c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/initializable.rb:30:in `run'
from c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/initializable.rb:55:in `block in run_initializers'
from c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/initializable.rb:54:in `each'
from c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/initializable.rb:54:in `run_initializers'
from c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/application.rb:136:in `initialize!'
from c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/railtie/configurable.rb:30:in `method_missing'
from C:/Users/example/Documents/Workspace/app/config/environment.rb:5:in `<top (required)>'
My environment.rb file is as follows...
# Load the rails application
require File.expand_path('../application', __FILE__)
# Initialize the rails application
App::Application.initialize!
I previously had resque with redis working with mongoid in this app, so its a sidekiq issue. I don't have a database.yml file since I am using mongoid. I also added the steps for kiqstand but I am getting the same error. Unsure how to resolve?
Edit: I'm 90% sure its because Sidekiq is looking for ActiveRecord, however I removed ActiveRecord cause I'm using Mongo instead. My application.rb has...
# remove activerecord and run off of mongo only
#require 'rails/all'
#http://stackoverflow.com/a/9327651/1026266
%w(
action_controller
action_mailer
active_resource
rails/test_unit
sprockets
).each do |framework|
begin
require "#{framework}/railtie"
rescue LoadError
end
end
Now I just need to figure out how to tell sidekiq to not look for it?
Lack of ActiveRecord was the issue, looks like there was a pull request - https://github.com/mperham/sidekiq/pull/1090
But that was rejected, so I just had to add ActiveRecord to my app and add a dummy database.yml file.

can't create object instace using irb

I try to create User instance of my user model in irb. but it gives me this error
1.9.3p194 :001 > u = User.new
NameError: uninitialized constant User
from (irb):1
from /home/darshana/.rvm/rubies/ruby-1.9.3-p194/bin/irb:16:in `<main>'
any help?
irb doesn't loads rails for you.
try bundle exec rails console

ACL9 and Rails 2.3.8 - NameError: uninitialized constant User::Role

I am running RVM (Ruby 1.8.7-head, Rails 2.3.8) and have the BASICS of a new app running. I have Authlogic working, with the minimal code to make that work. I am attempting to use ACL9 (which I have working on a different project, same RVM gemset)
class User < ActiveRecord::Base
# authentication
acts_as_authentic
# authorization
acts_as_authorization_subject
def full_name
"#{self.first_name} #{self.last_name}"
end
end
When I attempt to just run a simple check to make sure ACL9 is working properly...
script/console
u = User.first
u.has_role?(:anyrole)
I get this error...
ruby-1.8.7-head > u.has_role?(:anyrole)
NameError: uninitialized constant User::Role
from /Users/development/.rvm/gems/ruby-1.8.7-head#rails238/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:105:in `const_missing'
from /Users/development/.rvm/gems/ruby-1.8.7-head#rails238/gems/activerecord-2.3.8/lib/active_record/base.rb:1:in `compute_type'
from /Users/development/.rvm/gems/ruby-1.8.7-head#rails238/gems/activesupport-2.3.8/lib/active_support/core_ext/kernel/reporting.rb:11:in `silence_warnings'
from /Users/development/.rvm/gems/ruby-1.8.7-head#rails238/gems/activerecord-2.3.8/lib/active_record/base.rb:2230:in `compute_type'
from /Users/development/.rvm/gems/ruby-1.8.7-head#rails238/gems/activerecord-2.3.8/lib/active_record/reflection.rb:156:in `send'
from /Users/development/.rvm/gems/ruby-1.8.7-head#rails238/gems/activerecord-2.3.8/lib/active_record/reflection.rb:156:in `klass'
from /Users/development/.rvm/gems/ruby-1.8.7-head#rails238/gems/activerecord-2.3.8/lib/active_record/reflection.rb:187:in `quoted_table_name'
from /Users/development/.rvm/gems/ruby-1.8.7-head#rails238/gems/activerecord-2.3.8/lib/active_record/associations/has_and_belongs_to_many_association.rb:102:in `construct_sql'
from /Users/development/.rvm/gems/ruby-1.8.7-head#rails238/gems/activerecord-2.3.8/lib/active_record/associations/association_collection.rb:21:in `initialize'
from /Users/development/.rvm/gems/ruby-1.8.7-head#rails238/gems/activerecord-2.3.8/lib/active_record/associations/has_and_belongs_to_many_association.rb:5:in `initialize'
from /Users/development/.rvm/gems/ruby-1.8.7-head#rails238/gems/activerecord-2.3.8/lib/active_record/associations.rb:1306:in `new'
from /Users/development/.rvm/gems/ruby-1.8.7-head#rails238/gems/activerecord-2.3.8/lib/active_record/associations.rb:1306:in `role_objects'
from /Users/development/.rvm/gems/ruby-1.8.7-head#rails238/gems/acl9-0.12.0/lib/acl9/model_extensions/for_subject.rb:39:in `has_role?'
from (irb):2
I'm not sure why this works with every other app I have, but not this one - and I'm not sure what the error message is saying.. any help is appreciated.
class Role < ActiveRecord::Base
acts_as_authorization_role
end

Resources