Now that Rails 3 beta is out, I thought I'd have a look at rewriting an app I have just started work on in Rails 3 beta, both to get a feel for it and get a bit of a head-start. The app uses MongoDB and MongoMapper for all of its models and therefore has no need for ActiveRecord. In the previous version, I am unloading activerecord in the following way:
config.frameworks -= [ :active_record ] # inside environment.rb
In the latest version this does not work - it just throws an error:
/Library/Ruby/Gems/1.8/gems/railties-3.0.0.beta/lib/rails/configuration.rb:126:in
`frameworks': config.frameworks in no longer supported. See the generated
config/boot.rb for steps on how to limit the frameworks that will be loaded
(RuntimeError)
from *snip*
Of course, I have looked at the boot.rb as it suggested, but as far as I can see, there is no clue here as to how I might go about unloading AR. The reason I need to do this is because not only is it silly to be loading something I don't want, but it is complaining about its inability to make a DB connection even when I try to run a generator for a controller. This is because I've wiped database.yml and replaced it with connection details for MongoDB in order to use this gist for using database.yml for MongoDB connection details. Not sure why it needs to be able to initiate a DB connection at all just to generate a controller anyway....
Is anyone aware of the correct Rails 3 way of doing this?
I'm going by this from reading the source, so let me know if it actually worked. :)
The rails command that generates the application template now has an option -O, which tells it to skip ActiveRecord.
If you don't feel like rerunning rails, you should check the following in your existing app:
Check that your config/application.rb doesn't have require 'rails/all' or require "active_record/railtie". Instead, for a standard Rails setup without ActiveRecord, it should have only the following requires:
require File.expand_path('../boot', __FILE__)
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "rails/test_unit/railtie"
require "sprockets/railtie"
# Auto-require default libraries and those for the current Rails environment.
Bundler.require :default, Rails.env
If, in config/application.rb, you are using the config.generators section, make sure it doesn't have the line g.orm :active_record. You can set this explicitly to nil, if you want, but this should be the default when g.orm is completely omitted.
Optional, but in your Gemfile, remove the gem line that loads the module for your database. This could be the line gem "mysql" for example.
Rails 4
I was looking for how to disable it in rails 4 and only found this answer which no longer works in rails 4. So this is how you can do it in rails 4 (tested in RC1).
In a new project
rails new YourProject --skip-active-record
In an existing project
In your Gemfile, remove the database driver gem, e.g. gem 'sqlite3' or gem 'pg'.
In config/application.rb, replace require 'rails/all' with
require "action_controller/railtie"
require "action_mailer/railtie"
require "sprockets/railtie"
require "rails/test_unit/railtie"
In config/environments/development.rb, remove or comment out config.active_record.migration_error = :page_load
Potentially you have to remove active_record helpers from the spec_helper (via VenoM in the comments)
Potentially you have to remove the ConnectionManagement middleware (seems to be the case with unicorn): config.app_middleware.delete "ActiveRecord::ConnectionAdapters::ConnectionManagement" (via https://stackoverflow.com/a/18087332/764342)
I hope this helps others looking for how to disable ActiveRecord in Rails 4.
For a new rails app, you can have it exclude active record by specifying the --skip-active-record parameter. Eg:
rails new appname --skip-active-record
If you generated a new project using Rails 3.2, you will also need to comment out:
config.active_record.mass_assignment_sanitizer = :strict
and
config.active_record.auto_explain_threshold_in_seconds = 0.5
in your development.rb file.
All of the above are true. The one more thing which I had to do in rails 3.1 is to comment out
config.active_record.identity_map = true
in config/application.rb.
If you're running rspec, you also need to remove (in spec_helper):
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
and remove
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
Also comment out
# config/application.rb
config.active_record.whitelist_attributes = true
(noted on rails 3.2.13)
Related
I want to disable Rails from trying to obtain a connection to my Postgres database because my application no longer should rely on this database.
I'm currently in a transition from using Postgres to using a NoSQL database such as Mongo. I have most of the code migrated but I'm currently running a shadow write to Postgres for backwards compatibility reasons.
Is it sufficient to remove the ActiveRecord middlewares and shadow write code so that if Postgres were to fail or the connection pool stalled the Rails application would experience no timeout or downtime?
To remove ActiveRecord from your application, do the following:
In config/application.rb, change this:
require 'rails/all'
To this:
require "active_model/railtie"
require "active_job/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"
In environments/development.rb, remove this line, and any others that reference config.active_record that you may have added:
config.active_record.migration_error = :page_load
In environments/production.rb remove this line and any others that reference config.active_record that you may have added:
config.active_record.dump_schema_after_migration = false
If you have a file at config/initializers/new_framework_defaults.rb, remove the following line from it:
Rails.application.config.active_record.belongs_to_required_by_default = true
Finally, remove any references to ActiveRecord, such as in any models, migrations, or schema.rb, if still present. It's probably best to nuke your db folder entirely.
Note: If you're no longer using any of ActiveModel you can also remove it from the requires in application.rb.
To remove support and functionality for connecting to your Postgres database, remove the file config/database.yml and remove the gem 'pg' declaration from your Gemfile (then run bundle install).
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'
When I create a rails app I usually do it like:
rails new <app> --skip-test-suite
No I have an app where I forgot to add the param --skip-test-suite and I would want to use RSPEC instead of the default test suite. How can I get rid of the default? Can I just remove the test directory and what gems would I need to get rid of?
If you are using Rails 3, to totally remove TestUnit from your application, remove this line from your config/application.rb
require "rails/test_unit/railtie"
And add this to config/application.rb
config.generators do |g|
g.test_framework :rspec
end
I'm writing my first Rails app. The app uses a lot of enumerations, so I'd like to include this gem that makes it easier to work with them.
I'm stumped by the installation instructions, though, which say
[...] For a rails application configure the gem in the config block of
the config/environment.rb file
config.gem "enumerated_attribute"
In my config/environment.rb I don't see anything that looks like a "config block".
config/environment.rb:
# Load the rails application
require File.expand_path('../application', __FILE__)
# Initialize the rails application
Webtet::Application.initialize!
So what does the author mean when he writes "configure the gem in the config block"?
Does he just want me to include this line in config/environment.rb
config.gem "enumerated_attribute"
?
Looks like this setup instruction was written for rails 2 application. If this gem works with rails 3 you just should add gem 'enumerated_attribute' to your Gemfile. Also you can try to use https://github.com/brainspec/enumerize gem (it works with rails 3 and has SimpleForm, Formtastic support and other awesome features)
I have created a Rails 3 project with mentioned option. Can I somehow "revert" this option and have active record back as in default, or must I create a new project?
EDIT
Ok, to have ActiveRecord support back I had to:
Delete separate require calls in application.rb and replace it with require 'rails/all' (or just uncomment # require "active_record/railtie")
Uncomment # gem 'sqlite3-ruby', :require => 'sqlite3' line in Gemfile (for sqlite)
Create database.yml file and fill it with options
invoke rake db:create task
After that it seems that I can continue to work with AR as usual.
Make a new project with ActiveRecord, and then pairwise diff config/application.rb and config/environments/*.rb to determine if there are any default settings you should add back to your project.