How can I use methods like assert_equal from the console?
$ bundle exec rails console
Loading development environment (Rails 7.0.2.2)
[1] pry(main)> assert_equal(1, 1)
NoMethodError: undefined method `assert_equal' for main:Object
Related
While migrating from mina 0.3.8 to 1.2.3 the below code is giving error as
NoMethodError: undefined method `isolate' for main:Object
RUBY version - 2.6.2
task :deploy_all do
isolate do
domains.each do |domain|
set :domain, domain
invoke :deploy
run!
end
end
end
I have checked the source code, could not find isolate method which is available on 0.3.8.
Any suggestion on how to fix the issue ?
When I enter reload! in my Rails console using pry-rails, it doesn't appear to be working; i.e., the changes I made to my code aren't implemented. Everything I've read seems to say that the pry-rails gem implements reload! all on its own.
Here's how I test it: (I put Deal.hello in my code between pry lines 1 & 2)
// ♥ rails c
Running via Spring preloader in process 66760
Loading development environment (Rails 5.2.2)
[1] pry(main)> Deal.hello
NoMethodError: undefined method `hello' for Deal (call 'Deal.connection' to establish a connection):Class
from /Users/TuzsNewMacBook/.rvm/gems/ruby-2.3.7/gems/activerecord-5.2.2/lib/active_record/dynamic_matchers.rb:22:in `method_missing'
[2] pry(main)> Deal.hello
NoMethodError: undefined method `hello' for Deal (call 'Deal.connection' to establish a connection):Class
from /Users/TuzsNewMacBook/.rvm/gems/ruby-2.3.7/gems/activerecord-5.2.2/lib/active_record/dynamic_matchers.rb:22:in `method_missing'
[3] pry(main)> reload!
Reloading...
=> true
[4] pry(main)> Deal.hello
NoMethodError: undefined method `hello' for Deal (call 'Deal.connection' to establish a connection):Class
from /Users/TuzsNewMacBook/.rvm/gems/ruby-2.3.7/gems/activerecord-5.2.2/lib/active_record/dynamic_matchers.rb:22:in `method_missing'
[5] pry(main)> exit
[11:50:33] (deal-has-one-region) fares-you-can-use-rails
// ♥ rails c
Running via Spring preloader in process 66780
Loading development environment (Rails 5.2.2)
[1] pry(main)> Deal.hello
=> "hello"
[2] pry(main)>
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
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 try to create User instance of my user model in irb. but it gives me this error
1.9.3p194 :001 > u = User.new
NameError: uninitialized constant User
from (irb):1
from /home/darshana/.rvm/rubies/ruby-1.9.3-p194/bin/irb:16:in `<main>'
any help?
irb doesn't loads rails for you.
try bundle exec rails console