Adding back in ActiveRecord to Rails App - ruby-on-rails

I started a rails app using the -O (--skip-activerecord) configuration, but now I want to add it back.
I added require "rails/all" to my application.rb file, and added in a database.yml file. Unfortunately, it's not picking up the database when I initialize the application.
I hacked together a module which will connect to the database if I call it, but there has to be a better, more railsy way. A rails app created without the -O option doesn't need any additional code to make the connection.
What else do I need to do to get rails to automatically connect to the database without explicitly making a connection?

Add this line to your config/application.rb. If you already have it commented, uncomment it.
require "active_record/railtie"
Add database.yml and restart the server

It turns out that ActiveRecord 4 doesn't actually make the db connection until you call something. So just adding back in require "rails/all" and adding a database.yml does work. Just don't test it in the console without calling a method like Model.last.
Here's the rails issue explaining the functionality: https://github.com/rails/rails/issues/12804#issuecomment-29885300

Related

How do I remove active record from an existing rails 7 site?

I have an existing Rails 7 site, and turns out I won't use active record or a database of any kind in it. How can I remove everything related to databases to lighten the site? Should I even bother doing it?
I agree with #Eyeslandic, you can just ignore it, but if you really want, to delete it, the steps to do so are:
Remove a gem that is responsible for database handling (mysql2, psql, whatever you have in Gemfile)
Change your config/application.rb.
If you have require rails/all you will need to remove it, and require only the parts you do need. If you want to see what those are exactly, create a new rails app where in setup you skip active record, and copy the require statements from there. The full list of what rails/all require is here I think
Remove all config lines that go something likeconfig.active_record.... you find in the app
Delete your config/database.yml file, db/schema.rb and migrations
Adjust your test setup, remove database cleaners etc, this will depend on what test framework you have in the app (if any). Take fixtures into account if you have any
search config/environments/* for any mentons of active_record and database config

Reflect changes to environment.rb without restarting rails server

Rails server seems to need restart when I change config/environment.rb.
Is there a way to reflect the change without restarting server?
A gem for it or something like that exists?
The environment.rb file is one of the main initialiser files for starting your app and can't really be reloaded on each request because reloading that is nearly the same as restarting the whole app. I'm guessing there are some variables in there that you want to change without restarting every time. Well instead of putting them in there, you could create a .rb file in the lib directory. That code gets run when the app is started and is useful for defining custom classes, etc.
To make it automatically reload it on each request you need to use eager_load_paths in your config/application.rb. This question specifies how to do that.

Where do you put an object call that is needed before your application loads?

I have an object call that is basically initiating a singleton that is needed all over my application. I thought the best place to put this was the environment.rb file, right after the Application.initialize! call. And it had worked with all my tests. But now that I'm running my code in the development environment, I'm finding that it is not getting called. Is there somewhere else I should be putting it, or is there something wrong with my development environment setup?
EDIT: I haven't looked at the answers yet, but I did find out that it's not working in development when I have the config.cache_classes set to false.
Try putting it in an initializer file.
See this link for configuring rails.
Basically, I do use config files written in yml and load these with an initializer.
If you don't want to go into details, use Ryan Bate's gem: nifty-generators
and type in console:
rails g nifty:config
It will install everything for you. Then edit your config/app_config.yml file.

Ruby on Rails - how to reload classes?

I'm developing a Ruby on Rails app, and everytime I made changes to my class file, I need to restart the server in order for the code changes to be reflected. The code is in my controllers directory, but it's not a controller.
What change do I need to make to make the class reload automatically everytime I make a change? I've set caching to false in my environment config file, and it still doesn't work.
Any ideas?
I would probably move the code out of the controllers directory (if it isn't a controller it does not belong there) to maybe lib/controller_extensions/ and add this line to my config/application.rb (rails3) or to config/environment.rb (rails 2.3.10)
config.autoload_paths += Dir["#{config.root}/lib/controller_extensions/"]
It really depends on where the classes are originally loaded. Here is a method of reloading what you want in different environments.
Why does code need to be reloaded in Rails 3?
If its the development environment I don't think you have to change the server in order to get the changes made in the controller .Rather If there are any changes made in the Model class then you have to restart the server again.

Is environment.rb invoked on every http request?

I'm wondering what file I should use to ensure my initializing code will only be executed once when the application starts up. Is environment.rb the correct file to use or will it be invoked on every http request?
environment.rb is only loaded when the application is first started up. subsequent changes to the environment.rb file require a restart. What kind of code do you only want to execute once?
You might want to read through the Ruby on Rails guide for Configuring Rails Applications which talks about the different places to put initialization code.
Look at config/initializers for the recommended location custom startup code.
As far as possible leave environment.rb alone unless you're explicitly adding or changing items defined within the Rails::Initializer.run block.
If you want to manage custom settings across your various environments, e.g. you want production and development to have different settings for something, then the config/environments directory should be your first port of call.

Resources