I ran rails generate devise MODEL before rails generate devise:install - ruby-on-rails

I accidentally added my Devise model before running the initial Devise generator.
Code I ran first:
$ rails generate devise MODEL
Initial Devise generator (what I should have ran first):
$ rails generate devise:install
I now have a devise controller called Model that won't let me run 'rake db:migrate'
Error:
rake aborted!
User does not respond to 'devise' method. This usually means you haven't loaded your ORM file or it's being loaded too late. To fix it, be sure to require 'devise/orm/YOUR_ORM' inside 'config/initializers/devise.rb' or before your application definition in 'config/application.rb'
/Users/andyHuynh/.rvm/gems/ruby-1.9.3-p327/gems/devise-2.2.3/lib/devise/rails/routes.rb:443:in `raise_no_devise_method_error!'
/Users/andyHuynh/.rvm/gems/ruby-1.9.3-p327/gems/devise-2.2.3/lib/devise/rails/routes.rb:211:in `block in devise_for'
/Users/andyHuynh/.rvm/gems/ruby-1.9.3-p327/gems/devise-2.2.3/lib/devise/rails/routes.rb:207:in `each'
/Users/andyHuynh/.rvm/gems/ruby-1.9.3-p327/gems/devise-2.2.3/lib/devise/rails/routes.rb:207:in `devise_for'
/Users/andyHuynh/Code/jargon/config/routes.rb:3:in `block in <top (required)>'
/Users/andyHuynh/.rvm/gems/ruby-1.9.3-p327#global/gems/actionpack-3.2.12/lib/action_dispatch/routing/route_set.rb:282:in `instance_exec'
/Users/andyHuynh/.rvm/gems/ruby-1.9.3-p327#global/gems/actionpack-3.2.12/lib/action_dispatch/routing/route_set.rb:282:in `eval_block'
/Users/andyHuynh/.rvm/gems/ruby-1.9.3-p327#global/gems/actionpack-3.2.12/lib/action_dispatch/routing/route_set.rb:260:in `draw'
/Users/andyHuynh/Code/jargon/config/routes.rb:1:in `<top (required)>'
...
Is there a way I can undo this process to run the initial generator first? I am using Rails 3.2.12. Any help is appreciated. Thanks

You can undo generation commands with rails destroy ..., so in your case it would be:
$ rails destroy devise MODEL
If that doesn't work for whatever reason, you can just delete the model and migration - they're only files.

rails destroy model devise:user
or
rails destroy scaffold devise:user

Related

Ruby on Rails Error Using Rails Methods

I currently have started learning Ruby On Rails. I have Ruby 2.3.1 and Rails 5.1.1 on my Debian Linux. I was following the instructions here : http://railscasts.com/episodes/417-foundation?view=asciicast
However, when I try to use the method rails g scaffold product name price:decimal --skip-stylesheets. I get this error;
"/usr/lib/ruby/vendor_ruby/rails/railtie/configuration.rb:95:in `method_missing': undefined method `load_defaults' for #<Rails::Application::Configuration:0x000000016b9058> (NoMethodError)
from /root/Desktop/RubyOnRails/store/config/application.rb:12:in `<class:Application>'
from /root/Desktop/RubyOnRails/store/config/application.rb:10:in `<module:Store>'
from /root/Desktop/RubyOnRails/store/config/application.rb:9:in `<top (required)>'
from /usr/lib/ruby/vendor_ruby/spring/application.rb:82:in `require'
from /usr/lib/ruby/vendor_ruby/spring/application.rb:82:in `preload'
from /usr/lib/ruby/vendor_ruby/spring/application.rb:143:in `serve'
from /usr/lib/ruby/vendor_ruby/spring/application.rb:131:in `block in run'
from /usr/lib/ruby/vendor_ruby/spring/application.rb:125:in `loop'
from /usr/lib/ruby/vendor_ruby/spring/application.rb:125:in `run'
from /usr/lib/ruby/vendor_ruby/spring/application/boot.rb:18:in `<top (required)>'
from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'"
I get the error, also when I try to generate a controller or model. Because of this error, I can not follow any tutorial. I have tried to reinstall Rails but did not work.How can I solve this? Thank you in advanced.
Use
rails g scaffold product name price:decimal --no-stylesheets
instead --skip
You can do also in config/application.rb something like this.
config.generators do |g|
g.stylesheets false
end
You can see more details here http://guides.rubyonrails.org/configuring.html#configuring-generators
Try following steps:
1. rails new demo
2. cd demo
3. rake db:create
4. rails g scaffold product name:string price:float
5. rake db:migrate
6. rails server
I solved it by reinstalling Ruby and Rails again
Add this line to config/application.rb:
require 'neo4j/railtie'

uninitialized constant ActiveRecord::ConnectionAdapters::Mysql2Adapter::NATIVE_DATABASE_TYPES (NameError)

Im having a problem with running my rails server. ive set the abstract_mysql2_adapters at initializer that consist of:
class ActiveRecord::ConnectionAdapters::Mysql2Adapter
NATIVE_DATABASE_TYPES[:primary_key] = "int(11) auto_increment PRIMARY KEY"
end
and it solved my rake db:migrate problem, but when i try to run my rails server it gives me this error.
C:/Users/XXXX/Documents/RoRCmS/simple_cms/config/initializers/abstract_mysql2_adapter.rb:2:in <class:Mysql2Adapter>': uninitialized constant ActiveRecord::ConnectionAda
pters::Mysql2Adapter::NATIVE_DATABASE_TYPES (NameError)
from C:/Users/John/Documents/RoRCmS/simple_cms/config/initializers/abstract_mysql2_adapter.rb:1:in'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.0.0/lib/rails/engine.rb:609:in block (2 levels) in <class:Engine>'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.0.0/lib/rails/engine.rb:608:ineach'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.0.0/lib/rails/engine.rb:608:in block in <class:Engine>'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.0.0/lib/rails/initializable.rb:30:ininstance_exec'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.0.0/lib/rails/initializable.rb:30:in run'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.0.0/lib/rails/initializable.rb:55:inblock in run_initializers'
from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:228:in block in tsort_each'
from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:350:inblock (2 levels) in each_strongly_connected_component'
from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:431:in each_strongly_connected_component_from'
from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:349:inblock in each_strongly_connected_component'
from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:347:in each'
from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:347:incall'
from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:347:in each_strongly_connected_component'
from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:226:intsort_each'
from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:205:in tsort_each'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.0.0/lib/rails/initializable.rb:54:inrun_initializers'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.0.0/lib/rails/application.rb:215:in initialize!'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.0.0/lib/rails/railtie/configurable.rb:30:inmethod_missing'
from C:/Users/John/Documents/RoRCmS/simple_cms/config/environment.rb:5:in <top (required)>'
from C:/Users/John/Documents/RoRCmS/simple_cms/config.ru:3:inrequire'
from C:/Users/John/Documents/RoRCmS/simple_cms/config.ru:3:in block in <main>'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rack-1.5.5/lib/rack/builder.rb:55:ininstance_eval'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rack-1.5.5/lib/rack/builder.rb:55:in initialize'
from C:/Users/John/Documents/RoRCmS/simple_cms/config.ru:innew'
from C:/Users/John/Documents/RoRCmS/simple_cms/config.ru:in <main>'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rack-1.5.5/lib/rack/builder.rb:49:ineval'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rack-1.5.5/lib/rack/builder.rb:49:in new_from_string'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rack-1.5.5/lib/rack/builder.rb:40:inparse_file'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rack-1.5.5/lib/rack/server.rb:277:in build_app_and_options_from_config'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rack-1.5.5/lib/rack/server.rb:199:inapp'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.0.0/lib/rails/commands/server.rb:48:in app'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rack-1.5.5/lib/rack/server.rb:314:inwrapped_app'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.0.0/lib/rails/commands/server.rb:75:in start'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.0.0/lib/rails/commands.rb:78:inblock in '
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.0.0/lib/rails/commands.rb:73:in tap'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.0.0/lib/rails/commands.rb:73:in'
from bin/rails:4:in require'
from bin/rails:4:in'
I really need to solve this problem now.. thanks guys.
I solved it as follows. After importing a legacy sql dumpfile into MySQL with mysql -u username -p database_name < file.sql, I proceeded to perform a migration with bin/rake db:migrate, but I encountered the error Mysql2::Error: All parts of a PRIMARY KEY must be NOT NULL; if you need NULL in a key, use UNIQUE instead.
So I tried the same monkey patch as you, as suggested by user pjg here https://github.com/rails/rails/pull/13247#issuecomment-32425844, but then I encountered your error
uninitialized constant ActiveRecord::ConnectionAdapters::Mysql2Adapter::NATIVE_DATABASE_TYPES (NameError)
I followed user John Geliberte's advice and updated my Gemfile with gem 'activerecord-native_db_types_override' and checked that I also had gem 'mysql2' (NOT 'activerecord-mysql2-adapter' which causes other errors) and ran bundle install.
I used the the documentation associated with the activerecord-native_db_types_override gem and applied property for NATIVE_DATABASE_TYPES mentioned in Rails ActiveRecord library file for abstract_mysql_adapter, such that I made the following changes:
I created config/initializers/abstract_mysql2_adapter.rb and added:
require 'active_record/connection_adapters/mysql2_adapter'
NativeDbTypesOverride.configure({
ActiveRecord::ConnectionAdapters::Mysql2Adapter => {
primary_key: "int(11) auto_increment PRIMARY KEY"
}
})
I created config/environment.rb and add the following to load the monkey patch:
require File.expand_path('../initializers/abstract_mysql2_adapter.rb', __FILE__)
I checked that config/database.yml contained 'adapter: mysql2' for the db connection I wanted to use.
I then ran bin/rake db:migrate and it successfully performed the migration.
I have made a pull request to the documentation for the ActiveRecord Native Database Types Override Gem.
It was much easier to downgrade from MySQL 5.7 to 5.5 instead though.
My system:
mysql -V is mysql Ver 14.14 Distrib 5.7.13
rails -v is 4.2.4
ruby -v is ruby 2.3.0p0
SOLVED: i added the gem : gem 'activerecord-native_db_types_override'
Try this code instead:
class ActiveRecord::ConnectionAdapters::Mysql2Adapter
def modify_types(types)
super
types[:primary_key] = "int(11) auto_increment PRIMARY KEY"
types
end
end

Ruby on Rails Michael Hartl Chapter 6.1.3

When loading the Rails console in sandbox as following the tutorial, i can't create a new user object ? I've got this message :
>> User.new
NameError: uninitialized constant User
from (irb):1
from /Users/Genosia/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/railties-4.1.5/lib/rails/commands/console.rb:90:in `start'
from /Users/Genosia/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/railties-4.1.5/lib/rails/commands/console.rb:9:in `start'
from /Users/Genosia/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/railties-4.1.5/lib/rails/commands/commands_tasks.rb:69:in `console'
from /Users/Genosia/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/railties-4.1.5/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
from /Users/Genosia/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/railties-4.1.5/lib/rails/commands.rb:17:in `<top (required)>'
from /Users/Genosia/code/sample_app/bin/rails:8:in `<top (required)>'
from /Users/Genosia/.rbenv/versions/2.1.2/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /Users/Genosia/.rbenv/versions/2.1.2/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from -e:1:in `<main>'
You must have a user class in your app/models folder.
You can use the rails generator for that:
bundle exec rails g model user name:string date_of_birth:date ...
That will create the model under app/models and the migration under db/migrate
To run the migrations run
bundle exec rake db:migrate
bundle exec rails c --sandbox
Then it should work.
Note that opening the console in sandbox mode will rollback all the database changes when you close it.
To have persistent changes run
bundle exec rails c

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.

Rails: User does not respond to 'devise' method” when running “rails generate devise:install"

I have been working on this issue for hours now and haven't seemed to find anything exactly like this on StackOverflow.
C:\Sites\isawyou>rake db:migrate
rake aborted!
User does not respond to 'devise' method. This usually means you haven't loaded
your ORM file or it's being loaded too late. To fix it, be sure to require 'devi
se/orm/YOUR_ORM' inside 'config/initializers/devise.rb' or before your applicati
on definition in 'config/application.rb'
C:/Sites/isawyou/config/routes.rb:2:in `block in <top (required)>'
C:/Sites/isawyou/config/routes.rb:1:in `<top (required)>'
C:2:in `rescue in execute_if_updated'
C:in `execute_if_updated'
C:/Sites/isawyou/config/environment.rb:5:in `<top (required)>'
Tasks: TOP => db:migrate => environment
(See full trace by running task with --trace)
Issue: Everytime I try to migrate I get this error. However, I do not have a devise.rb file installed so am not sure how to fix this..
Attempts to solve: tried this answer however that didn't let me populate the correct rake routes I needed (ie: new user sessions, create new user, etc..)
Tried redoing the order of commands (as shown below).. But that didn't work either.. I always get stuck at the "rake db:migrate" portion..
$ rails d devise User
$ rails generate devise:install (you may have to override previous files)
$ rails generate devise User
$ rake db:drop
$ rake db:create
$ rake db:migrate
$ rake routes
Thank you in advance if you know the fix!!
It looks like you have devise generator previously run unsuccessfully. Backtrace gives a clue that error happens within config/routes.rb line 2. I guess there is a following line of code, setting up devise routes
devise_for :users
But your users model doesn't have devise modules setup. There should be something like
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :timeoutable and :omniauthable
devise :database_authenticatable, :rememberable, :trackable, :validatable
If this line is present it means that devise is improperly installed.
Also there is a chance that you have another class or module "User" within ActiveSupport::Dependencies autoloader paths and when devise_for :users line requires User class this class/module loaded first, whereas your real User module is not loaded. The "invalid" user module/class is not ActiveRecord descendant, so devise AR hook is not installed to it, so it doesn't have #devise method. So make sure you don't have any other classes/modules named User

Resources