When -exactly- does the Rails3 application get initialized? - ruby-on-rails

I've been fighting left and right with rails 3 and bundler. There are a few gems out there that don't work properly if the rails application hasn't been loaded yet. factory_girl and shoulda are both examples, even on the rails3 branch.
Taking shoulda as an example, when trying to run rake test:units I get the following error:
DEPRECATION WARNING: RAILS_ROOT is deprecated! Use Rails.root instead. (called from autoload_macros at c:/code/test_harness/vendor/windows_gems/gems/shoulda-2.10.3/lib/shoulda/autoload_macros.rb:40)
c:/code/test_harness/vendor/windows_gems/gems/shoulda-2.10.3/lib/shoulda/autoload_macros.rb:44:in 'join': can't convert #<Class:0x232b7c0> into String (TypeError)
from c:/code/test_harness/vendor/windows_gems/gems/shoulda-2.10.3/lib/shoulda/autoload_macros.rb:44:in 'block in autoload_macros'
from c:/code/test_harness/vendor/windows_gems/gems/shoulda-2.10.3/lib/shoulda/autoload_macros.rb:44:in 'map'
from c:/code/test_harness/vendor/windows_gems/gems/shoulda-2.10.3/lib/shoulda/autoload_macros.rb:44:in 'autoload_macros'
from c:/code/test_harness/vendor/windows_gems/gems/shoulda-2.10.3/lib/shoulda/rails.rb:17:in '<top (required)>'
Digging a bit deeper into lib/shoulda/rails, I see this:
root = if defined?(Rails.root) && Rails.root
Rails.root
else
RAILS_ROOT
end
# load in the 3rd party macros from vendorized plugins and gems
Shoulda.autoload_macros root, File.join("vendor", "{plugins,gems}", "*")
So...what's happening here is while Rails.root is defined, Rails.root == nil, so RAILS_ROOT is used, and RAILS_ROOT==nil, which is then being passed on to Shoulda.autoload_macros. Obviously the rails app has yet to be initialized. With Rails3 using Bundler now, there's been some hubub over on the Bundler side about being able to specify an order in which the gems are required, but I'm not sure whether or not this would solve the problem at hand.
Ultimately my questions is this: When exactly does the environment.rb file (which actually initializes the application) get pulled in? Is there any harm to bumping up when the app is initialized and have it happen before the Bundler.require line in config/application.rb? I've tried to hack bundler to specify the order myself, and have the rails gem pulled in first, but it doesn't appear to me that requiring the rails gem actually initializes the application.
As this line (in config/application.rb) is being called before the app is initialized, any gem in the bundler Gemfile that requires rails to be initialized is going to tank.
# Auto-require default libraries and those for the current Rails environment.
Bundler.require :default, Rails.env

Well, it was actually fairly easy to trace this down. All the rails libraries are being pulled in in application.rb. The app itself is being initialized in environment.rb.

Related

Why is too hard to create a simple proyect with rails?

Only i want to create a simple proyect with ruby and rails, but i think its harder than it looked.
I have a version of ruby 2.6.8, i created a new project with this command rails new blog, after that cd to blog
i applied this command: bin/rails server
i am facing this error: bin/rails:4:in require_relative': cannot load such file -- .../rb/blog/config/boot (LoadError) from bin/rails:4:in '
check with command:
ruby -v
that you ruby version same as in gemfile file, also as mention before don't forget run "bundle" after creating new project or add new gem to gemfile.
If this didn't help and you rails version is 6 you can manually fix problem - create boilerplate config/boot.rb:
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
require 'bundler/setup' # Set up gems listed in the Gemfile.
require 'bootsnap/setup' # Speed up boot time by caching expensive operations.
i am only start learning rails but i think if you only beginner then try fix problem in another way such reinstall rails, ruby etc. bc in future it can cause problem, its not normal when you just create new project and it already dont work, something wrong.

What can be done to fix the following error when we run any rails command: " `require_relative': cannot load such file "

Any rails command doesn't work for me. I have several versions of ruby installed through rvm. I tried installing rails with all the versions, they do install successfully but with all of them I face the following error whenever I run any rails command in my project directory:
~ rails new blog
Traceback (most recent call last):
1: from bin/rails:3:in `<main>'
bin/rails:3:in `require_relative': cannot load such file -- /Users/Am33d/Documents/config/boot (LoadError)
I tried looking up for the error but didn't find any solutions.
What can be done to fix this? I am using macOS Mojave (10.14.6)
This error would indicate that you do not have a boot.rb file in your config directory for some reason. When running rails commands -- regardless of if you run them as bin/rails [command] or bundle exec rails [command], runs the bin/rails file. This file typically has a line require_relative '../config/boot. The boilerplate bin/rails file in a new Rails 6 app is:
#!/usr/bin/env ruby
begin
load File.expand_path('../spring', __FILE__)
rescue LoadError => e
raise unless e.message.include?('spring')
end
APP_PATH = File.expand_path('../config/application', __dir__)
require_relative '../config/boot'
require 'rails/commands'
To simply fix this you can create a blank file by running touch config/boot.rb from the root directory of your application and that would itself suppress the error. Having said that, you'd be better off creating a config/boot.rb file that contains useful information. The boilerplate config/boot.rb file is this
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
require 'bundler/setup' # Set up gems listed in the Gemfile.
require 'bootsnap/setup' # Speed up boot time by caching expensive operations.
Without this file you are not necessarily appropriately loading the gems from your gemfile or caching operations with bootsnap.
When I ran into this problem, I also received the same error when trying to use rails -s.
If the same is happening for you, its because your version of ruby isn't compatible with your rails version.
After upgrading to the latest rails version the error stopped and everything worked well.
A little bit weird.
rails new blog should not need to find boot file, neither need bundler. Actually, you are trying to create a new project, so it is expected there is not Gemfile, boot, or bundler created for your project.
So I would advise you to find what rails command is being executed. Sometimes one rails executable created by bundler is taking precedence in the $PATH environment variable.
Ex: some incorrect bin/rails executable is on your $HOME directory.
i was trying to build an app using 'rails server' and this error was showing. You have to make sure that the rails version matches the ruby version and even if you do what an answer above said (create the config/boot.rb paste) doesnt work, than you have to change the version of rails to the stable. I'll let the link of this problem here, there's an issue closed in github for this problem https://github.com/rails/rails/pull/43951
to solve the problem you have to replace the gem rails line on the gemfile to this:
gem "rails", github: "rails/rails", branch: "7-0-stable"
Sorry about my english.

How to install pry/pry-rails globally for all projects?

How can replace irb and rails console with pry/pry-console globally for every project without having to include it in a project?
Easy mode is to hack your .irbrc, so that when anything tries to load IRB, you take over and force it to load Pry instead:
begin
gem "pry"
rescue => ex
$stderr.puts ex.message
else
require "pry"
Pry.start
exit!
end
If you're using Bundler, however, that'll still only work if Pry is available in the current bundle.
To make it work even when Bundler doesn't think Pry should be allowed to activate, you'll need to monkey with gem loading -- Bundler tries very hard to make it impossible.
To do that, you'll need a ~/.rubyrc.rb file, which you ensure always gets loaded for all ruby commands by exporting RUBYOPT=$HOME/.rubyrc in your .bashrc / .zshrc.
That file can then hack into Bundler's internals to force loading a non-bundled gem. You can also monkey-patch Bundler.require, which is how Rails loads all the gems in the Gemfile, to similarly force pry-rails in any application that contains rails.
(This strategy also allows you to globally support binding.pry, without needing to explicitly require anything, or add the gem to the project.)

rails: NoMethodError in production only

I am on hour 12 of programming, so i might be overlooking something simple, but any suggestions on this issue?
in my app_helper i added a module for a redcarpet filter with haml
module Haml::Filters::Redcarpet
include Haml::Filters::Base
include ActionView::Helpers::TagHelper
def render(text)
options = [:autolink, :smart, :hard_wrap, :no_intraemphasis]
content_tag(:div, Redcarpet.new(text.to_s, *options).to_html.html_safe, :class => "markup" )
end
end
works great in development. but in production, it's throwing
[ !EXCEPTION! ] NoMethodError: undefined method 'content_tag' for Haml::Filters::Redcarpet:Module
why would that be? or what can i check? i even ran the console on production and was able to include TagHelper and use those methods. puzzled...
My guess is that you have different versions of one or more gems on your production box from your dev environment. Do "gem list" in both and add the results to your original post.
The best way to avoid this problem is to use the Ruby Version Manager (RVM) and the 'bundler' gem: you can create a gemset specific to your project and thus ensure that the gems are exactly the same for both versions (prod & dev) of the project.
rvm: https://rvm.io
bundler: http://gembundler.com/
Oh - and if you look down your stack trace you will probably see a reference to the gem that sets up the haml/redcarpet stuff, this is probably the culprit, ie has a different version.

Where are required gems defined?

In my rails application I once used authlogic-oid and ruby-openid. Now I want to get rid of them and I removed both gems and also their config.gem lines from my environment.rb.
Although my application works, I can't do any database migrations because I get a "Missing these required gems" error. Also if I run rake gems:install these gems are re-installed.
Where are the references to the gems stored?
The standard way to define a gem dependency is in the environment configuration. It usually takes place in the environment.rb file for any environment, but some gems might be specified also per-environment. Check the environment files in config/environments.
Also make sure some file doesn't include the gem with the classic RubyGems gem command.
Finally, check these gems are not required by other gems or plugins used by your application.

Resources