I'm creating standalone rails engine application which will be further added to main application, using mongoid. So, I started like this.
rails plugin new some_engine --mountable --skip-active-record
Then in the gem file:
gem 'mongoid', "~> 3.0.15"
Then I run this command for generating mongo config file:
some_engine\test\dummy>rails g mongoid:config
which generate mongoid.yml under test/dummy/config folder
Now if I generate some model it's still invoking active record
some_engine>rails generate scaffold post title:string
it gives output:
invoke active_record
create db/migrate/20121219170013_create_some_engine_posts.rb
...........
Then I change the rails file under script/rails folder as it's having:
require 'rails/all'
to
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "sprockets/railtie"
require "rails/test_unit/railtie"
Then if I generate something it still not invokes mongoid
some_engine>rails generate model post
Nothing happens here.
Also when I run
some_engine>rails generate scaffold post title:string
invoke resource_route.....
Is that I'm missing something here to use mongoid with Rails Engine?
It sounds like mongoid still isn't loaded. I'm sorry, but I don't have time to reproduce. What happens if you explicitly require mongoid in the engine.rb file?
In Rails 4 if we do not need active_resource.The generated plugin app has rails modules loaded in bin/rails.
require "action_controller/railtie"
require "action_mailer/railtie"
require "sprockets/railtie"
require "rails/test_unit/railtie"
require "mongoid"
require 'rails/engine/commands'
Related
Summary
I am converting a Rails project from RSPec to Minitest and want my Rails project generator to create minitest test files.
In detail
I have a Rails project which I generated with skipped tests:
rails new example --skip-test-unit
I then added RSpec (following the standard rails generate rspec:install). Then I thought I'd like to try minitest instead.
I made a project doppelganger and copied test directory into my rails project:
rails new example # so we have test unit now
While I can run minitest tests the generator does not make the files for me.
rails g model Contact --skip-migration
Running via Spring preloader in process 13814
invoke active_record
create app/models/contact.rb
# no test files!
I have tried:
Reading through stackoverflow - everyone is converting it into RSpec
Stop and started Spring
commenting out a line in application.rb config.generators.system_tests = nil
How can I fix this to get minitest files generated?
I diffed a new application with my current application and application.rb's requires were very different.
App not generating test files
# rails/config/application.rb
require "rails"
# Pick the frameworks you want:
require "active_model/railtie"
require "active_job/railtie"
require "active_record/railtie"
require "active_storage/engine"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_mailbox/engine"
require "action_text/engine"
require "action_view/railtie"
require "action_cable/engine"
require "sprockets/railtie"
# require "rails/test_unit/railtie" <= generator left test_unit commented out
App Generating test files
# rails/config/application.rb
require "rails/all"
I made the require statements in my non-generating app like the generating app. ran spring stop and ran rails generator which now worked.
rails g model Contact --skip-migration
Running via Spring preloader in process 16286
invoke active_record
create app/models/contact.rb
invoke test_unit
create test/models/contact_test.rb
invoke factory_bot
create test/factories/contacts.rb
My project was created using PostgresSQL but we really intend to use MongoDB, what should I change in order for that to happen?
A lot of scaffolding already took place, so we are trying to savage any of the work already done...
Remove database adapter gems from your Gemfile (mysql2, sqlite3, etc.)
Change your config/application.rb
Remove require 'rails/all' line and require frameworks you want to use, for example:
require "action_controller/railtie"
require "action_view/railtie"
require "action_mailer/railtie"
require "active_job/railtie"
require "action_cable/engine"
require "sprockets/railtie"
require "rails/test_unit/railtie"
NOTE: You should be using the require snippet from the rails/all.rb file that is current with your version of Rails without the active_record railtie. Here is a link to rails/all.rb on the Rails master branch.
Delete your config/database.yml file, db/schema.rb and migrations (if any)
Delete migration check in test/test_helper.rb
Delete any ActiveRecord configuration from your config/environments files
Better late than never! This should come in handy to somebody, someday!!!!
The following python-based migration framework does the job.
https://github.com/datawrangl3r/pg2mongo
Coming to the performance, the migration of each JSON object is dynamic and there shouldn't be any memory lock issues when you use the above framework.
The same situation I also I have to face . I am adding some additional points to above answer .
1) Create a file in initializers and put this code
Mongoid.load!(Rails.root.join("config/mongoid.yml"))
2) You have to remove from every model which are inheriting from ApplicationRecord and delete the application_record file.
3) If you have installed devise so you have to change
**From **
require 'devise/orm/active_record'
to
require 'devise/orm/mongoid'
4) If you are using carrierwave so in gem file you have to replace
From
gem 'carrierwave', github: 'carrierwaveuploader/carrierwave'
To
gem 'carrierwave-mongoid', :require => 'carrierwave/mongoid'
I want to disable ActiveRecord in Rails 4. I did the following in config/application.rb
require File.expand_path('../boot', __FILE__)
# require 'rails/all' -- commented
require "action_controller/railtie"
require "action_mailer/railtie"
#require "active_resource/railtie" no need
#require "rails/test_unit/railtie" no need
#require "sprockets/railtie" no need
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env)
module MyApp
class Application < Rails::Application
config.app_middleware.delete "ActiveRecord::ConnectionAdapters::ConnectionManagement"
end
end
By I have an error of
/home/alex/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/railtie/configuration.rb:95:in
method_missing: undefined method active_record for #<Rails::Application::Configuration:0x00000002005c38> (NoMethodError)
If you are creating a new application, you can use -O to skip ActiveRecord:
rails new my_app -O
For existing applications:
1. Remove database adapter gems from your Gemfile (mysql2, sqlite3, etc.)
2. Change your config/application.rb
Remove require 'rails/all line and require frameworks (among those available in your rails version, the list varies, do not just copy) you want to use, for example:
require "action_controller/railtie"
require "action_mailer/railtie"
require "sprockets/railtie"
require "rails/test_unit/railtie"
Remove config.active_record.raise_in_transactional_callbacks = true from config/application.rb
3. Delete your config/database.yml file, db/schema.rb and migrations (if any)
4. Delete migration check in test/test_helper.rb
5. Delete any ActiveRecord configuration from your config/environments files (this is what is causing your error)
This is all you need to do for an empty Rails app. If you run into problems caused by your existing code, stack trace should give you sufficient information on what you need to change. You might for example have some ActiveRecord configuration in your initializers.
Hi this is what the default rails new new_app -O gives
require "rails"
# Pick the frameworks you want:
require "active_model/railtie"
require "active_job/railtie"
# require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_view/railtie"
require "sprockets/railtie"
require "rails/test_unit/railtie"
inside your config/application.rb
Additionally, it comes without database.yml and NO db/migrate/* and schema.rb
Since this is still the first hit when searching Google for disabling active record for Rails 5, I'll add this here:
For Rails 5
Do all the steps in #mechanicalfish answer, but also remove the line
Rails.application.config.active_record.belongs_to_required_by_default = true
from
config/initializers/new_framework_defaults.rb
For those using the rails-api gem you may encounter a similar error when using the --skip-active-record flag when doing rails-api new my_api. The current fix (until a new corrected version of the gem is released) is to edit your rails-api gem to have this commit. Use bundle open and replace the old Gemfile with the new corrected one. Rerun and you should be all set.
For disable ActiveRecord in Rails 4.2 you may create config/initializers/middleware.rb
Rails.application.middleware.tap do |middleware|
middleware.delete ActiveRecord::Migration::CheckPending
middleware.delete ActiveRecord::ConnectionAdapters::ConnectionManagement
middleware.delete ActiveRecord::QueryCache
end
See the terminal rake middleware
For Rails 5:
If you are generating a new app
Use --skip-active-record option to generate an application without a database:
rails new myApp --skip-active-record
Notice the extra hyphen '-' as opposed to previous versions of Rails.
For Rails Plugins (or gems) with a spec/dummy app
When your rails app lives in spec/dummy and you start your server from the plugin-root directory. You might still getting following error:
Cannot load `Rails.application.database_configuration`: Could not load database configuration. No such file - ["config/database.yml"]
To avoid this, remove require rails/all inside the file bin/rails and require frameworks you want to use, for example:
# Pick the frameworks you want:
require "active_model/railtie"
require "active_job/railtie"
# require "active_record/railtie"
require "action_cable/engine"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_view/railtie"
require "sprockets/railtie"
require "rails/test_unit/railtie"
For Ruby On Rails version 5.1.x
require "rails"
# Pick the frameworks you want:
require "active_model/railtie"
require "active_job/railtie"
# require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_view/railtie"
require "action_cable/engine"
require "sprockets/railtie"
require "rails/test_unit/railtie"
I want to disable ActiveRecord in Rails 4. I did the following in config/application.rb
require File.expand_path('../boot', __FILE__)
# require 'rails/all' -- commented
require "action_controller/railtie"
require "action_mailer/railtie"
#require "active_resource/railtie" no need
#require "rails/test_unit/railtie" no need
#require "sprockets/railtie" no need
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env)
module MyApp
class Application < Rails::Application
config.app_middleware.delete "ActiveRecord::ConnectionAdapters::ConnectionManagement"
end
end
By I have an error of
/home/alex/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/railtie/configuration.rb:95:in
method_missing: undefined method active_record for #<Rails::Application::Configuration:0x00000002005c38> (NoMethodError)
If you are creating a new application, you can use -O to skip ActiveRecord:
rails new my_app -O
For existing applications:
1. Remove database adapter gems from your Gemfile (mysql2, sqlite3, etc.)
2. Change your config/application.rb
Remove require 'rails/all line and require frameworks (among those available in your rails version, the list varies, do not just copy) you want to use, for example:
require "action_controller/railtie"
require "action_mailer/railtie"
require "sprockets/railtie"
require "rails/test_unit/railtie"
Remove config.active_record.raise_in_transactional_callbacks = true from config/application.rb
3. Delete your config/database.yml file, db/schema.rb and migrations (if any)
4. Delete migration check in test/test_helper.rb
5. Delete any ActiveRecord configuration from your config/environments files (this is what is causing your error)
This is all you need to do for an empty Rails app. If you run into problems caused by your existing code, stack trace should give you sufficient information on what you need to change. You might for example have some ActiveRecord configuration in your initializers.
Hi this is what the default rails new new_app -O gives
require "rails"
# Pick the frameworks you want:
require "active_model/railtie"
require "active_job/railtie"
# require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_view/railtie"
require "sprockets/railtie"
require "rails/test_unit/railtie"
inside your config/application.rb
Additionally, it comes without database.yml and NO db/migrate/* and schema.rb
Since this is still the first hit when searching Google for disabling active record for Rails 5, I'll add this here:
For Rails 5
Do all the steps in #mechanicalfish answer, but also remove the line
Rails.application.config.active_record.belongs_to_required_by_default = true
from
config/initializers/new_framework_defaults.rb
For those using the rails-api gem you may encounter a similar error when using the --skip-active-record flag when doing rails-api new my_api. The current fix (until a new corrected version of the gem is released) is to edit your rails-api gem to have this commit. Use bundle open and replace the old Gemfile with the new corrected one. Rerun and you should be all set.
For disable ActiveRecord in Rails 4.2 you may create config/initializers/middleware.rb
Rails.application.middleware.tap do |middleware|
middleware.delete ActiveRecord::Migration::CheckPending
middleware.delete ActiveRecord::ConnectionAdapters::ConnectionManagement
middleware.delete ActiveRecord::QueryCache
end
See the terminal rake middleware
For Rails 5:
If you are generating a new app
Use --skip-active-record option to generate an application without a database:
rails new myApp --skip-active-record
Notice the extra hyphen '-' as opposed to previous versions of Rails.
For Rails Plugins (or gems) with a spec/dummy app
When your rails app lives in spec/dummy and you start your server from the plugin-root directory. You might still getting following error:
Cannot load `Rails.application.database_configuration`: Could not load database configuration. No such file - ["config/database.yml"]
To avoid this, remove require rails/all inside the file bin/rails and require frameworks you want to use, for example:
# Pick the frameworks you want:
require "active_model/railtie"
require "active_job/railtie"
# require "active_record/railtie"
require "action_cable/engine"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_view/railtie"
require "sprockets/railtie"
require "rails/test_unit/railtie"
For Ruby On Rails version 5.1.x
require "rails"
# Pick the frameworks you want:
require "active_model/railtie"
require "active_job/railtie"
# require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_view/railtie"
require "action_cable/engine"
require "sprockets/railtie"
require "rails/test_unit/railtie"
I am using mongoid and I want to get rid somehow from the active_record in the gemfile.lock
Can that be achieved? And How?
No you can't remove it from Gemfile.lock (it's a dependency of Rails4), but you can prevent it from being loaded by following the Mongoid installation documentation:
http://mongoid.org/en/mongoid/docs/installation.html (Getting rid of ActiveRecord section)
The relevant info:
Remove the require "rails/all" from config/application.rb and replace it with:
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "rails/test_unit/railtie"
# require "sprockets/railtie" # Uncomment this line for Rails 3.1+