Out of curiosity, where is Rails.application defined? If I do this in irb in a Rails-powered app, I got an error:
$ irb
1.9.3-p327 :001 > require 'rails/application'
=> true
1.9.3-p327 :002 > class App < Rails::Application; end
NoMethodError: undefined method `application' for Rails:Module
I have tried requiring more, like active_support and rails/railtie/configuration but no luck.
The purpose of this is to load a minimal Rails env where I can test an ActiveRecord::Base helper :)
Usually when working with Rails you use rails console instead of IRB. When you run rails console it will boot up your Rails application.
FWIW, Rails.application is defined in railties:
lib/rails.rb
Related
When I try to run any command that uses the Twitter API in the ruby console, I get the error NameError: uninitialized constant Twitter.
I have named this twitterFeed.rb because I read that it should not be named twitter.rb. This file is placed in my config/initializers folder. I have ran bundle install already, and the line gem 'twitter', '~> 6.2' is in my gem file.
require 'rubygems'
require 'bundler/setup'
require 'twitter'
require 'json'
client = Twitter::REST::Client.new do |config|
config.consumer_key = ENV['TWITTER_CONSUMER_KEY']
config.consumer_secret = ENV['TWITTER_CONSUMER_SECRET']
config.access_token = ENV['TWITTER_ACCESS_TOKEN']
config.access_token_secret = ENV['TWITTER_ACCESS_TOKEN_SECRET']
end
It looks like you're trying to use irb instead of the Rails console to run your code. When you run through irb (or pry), you aren't actually loading the Rails environment, so none of the gems will be available. You can manually require them, but you still won't have access to the Rails environment.
What you want to do instead is use rails console (or rails c for short).
For example with irb, Twitter isn't loaded:
rails_dir » irb
2.2.4 :001 > Twitter
NameError: uninitialized constant Twitter
from (irb):1
from /Users/bbugh/.rvm/rubies/ruby-2.2.4/bin/irb:11:in `<top (required)>'
With rails c, it works just fine:
rails_dir » rails c
Loading development environment (Rails 5.0.1)
2.2.4 :001 > Twitter
=> Twitter
You can take all of those requires out of your initializer - Rails will require the gems automatically by that point. You just need to use rails console when you're doing console work with Rails.
I am using Sidekiq in one of my projects. Now I need to clear a queue,
the RetrySet, to be more specific.
Following this page from Sidekiq's Github manual, this should work:
Loading development environment (Rails 4.2.1)
>> Sidekiq::RetrySet.new.clear
NameError: uninitialized constant Sidekiq::RetrySet
But it doesn't. Sidekiq itself seems to be loaded:
>> Sidekiq
=> Sidekiq
What am I doing wrong here?
EDIT:
Using Sidekiq version 3.3.4
Looks like you need to require the api library explicitly.
require 'sidekiq/api'
See this for more info https://github.com/mperham/sidekiq/issues/1732
See https://github.com/mperham/sidekiq/blob/master/lib/sidekiq/api.rb#L612
The inheritance explaination
class SortedSet
...
def clear
...
end
end
class JobSet < SortedSet
...
end
class RetrySet < JobSet
...
end
However, In my rails console it worked without needing me to require the library. It was already required. see
> require 'sidekiq/api'
=> false
I use Sidekiq 4.0.1
> Sidekiq::VERSION
=> "4.0.1"
I have an initializer in my rails app which I don't want to be started with rails console or rake task.
I put this code into my initializer:
puts "start"
if defined?(Rails::Console)
puts "console"
elsif File.basename($0) == "rake"
puts "rake"
end
puts "end"
When I run 'rails console' I get this:
$ rails console
start
end
Loading development environment (Rails 4.2.0.beta2)
[1] pry(main)>
But inside console:
[1] pry(main)> defined?(Rails::Console)
=> "constant"
However, it works for rake:
$ bundle exec rake routes
start
rake
end
Why it works inside console, but doesn't in initializer?
Or is there a better way to determine console/rake inside initializer?
I would like to know if when using pry is possible to have access to the variable app?
As an example, when I try to access the root_path I get the following error:
[14] pry(main)> app.root_path
NameError: undefined local variable or method `app' for main:Object
Someone said that "It does now work with pry and 3.2.9". I am using rails 3.2.12, but it doesn't seem to work.
I have gem 'pry' in my GemFile group development and in config/environments/development.rb the following
# Use Pry instead of IRB
silence_warnings do
begin
require 'pry'
IRB = Pry
rescue LoadError
end
end
Yes, it works
➜ MyApp git:(master) rc
Loading development environment (Rails 3.2.13)
[1] pry(main)> app.root_path
=> "/"
I use pry-rails in favor of your overriding of IRB in an initializer.
group :development do
gem 'pry-rails'
end
https://github.com/rweng/pry-rails
Though this is solved, if you don't want to or can't use the 'pry-rails' gem for some reason you can also add the following into your .pryrc file:
if defined?(Rails) && Rails.env
extend Rails::ConsoleMethods
end
You can read more in the pry wiki
The irb is giving true at first and then false always for the command require rails.
The rails console is giving false always.
How's this happening?
Please see below cmd-
~/Workspaces/eclipse/image_cropper_ws/image_cropper$ irb
1.9.2-p180 :001 > require 'rails'
=> true
1.9.2-p180 :002 > require 'rails'
=> false
1.9.2-p180 :003 > exit
~/Workspaces/eclipse/image_cropper_ws/image_cropper$ rails console
Loading development environment (Rails 3.2.8)
1.9.2-p180 :001 > require 'rails'
=> false
1.9.2-p180 :002 > require 'rails'
=> false
require returns false when what you're trying to require is already loaded - the first time you require 'rails', it's not loaded, and require returns true.
The second time you require 'rails', it is already loaded and require returns false.
Rails is always loaded in the rails console.
Check the docs for require, it states
Loads the given name, returning true if successful and false if the feature is already loaded.
So the first time you call require in irb it loads and returns true. The second time it is already loaded so it returns false.
When you call rails c it loads irb with your rails environment so it must have already required rails