Pry-rails not reloading correctly - ruby-on-rails

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)>

Related

Call rails' assert_equal from console

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

undefined method `default' for ActionMailer

I'm having this weird error with Rails 5 action mailer.
# app/mailers/application_mailer.rb
class ApplicationMailer < ActionMailer::Base
default from: "from#example.com"
layout 'mailer'
end
irb
~/workspace/rails5 > rails c ruby-2.4.1#rails5
Running via Spring preloader in process 45498
Loading development environment (Rails 5.1.2)
2.4.1 :001 > ApplicationMailer
NameError: uninitialized constant Mail::TestMailer
from app/mailers/application_mailer.rb:1:in `<top (required)>'
from (irb):1
2.4.1 :002 > ApplicationMailer
NoMethodError: undefined method `default' for ApplicationMailer:Class
from app/mailers/application_mailer.rb:3:in `<class:ApplicationMailer>'
from app/mailers/application_mailer.rb:1:in `<top (required)>'
from (irb):2
2.4.1 :003 >
This is the default Scaffold of rails 5.
Two really weird things happen the first is that it always tells me the uninitialized constant then trows default is not a method right after.
Just wondering how to fix it or if I should downgrade to Rail 5.0
The solution turned out to be that if you have a model named mail the ActionMailer will grab that one first instead of the rails class. If this happens then it fails the first time but the second time will load the rails class and try and smash them together. Allowing the submodule to work but treating the main class as a monkey path.

How do I use the Rails toggle?

I would like to be able to use toggle but not quite sure how to do this.
I have a boolean attribute called deceased on my Member model.
Yet when I try to toggle it with toggle(attribute) it doesn't work.
This is what happens at the command-line:
[41] pry(main)> member.deceased.toggle!
NoMethodError: undefined method `toggle!' for true:TrueClass
from (pry):41:in `__pry__'
[42] pry(main)> member.toggle(deceased)
NameError: undefined local variable or method `deceased' for main:Object
from (pry):42:in `__pry__'
[43] pry(main)> member.deceased.toggle
NoMethodError: undefined method `toggle' for true:TrueClass
from (pry):43:in `__pry__'
[44] pry(main)> member.deceased
=> true
[45] pry(main)> toggle(member.deceased)
NoMethodError: undefined method `toggle' for main:Object
from (pry):45:in `__pry__'
How do I use toggle?
FYI: All of the above was done at the rails c...but I have pry installed. But given that toggle seems to be an ActiveRecord method, it shouldn't be an issue right?
You should be able to do it with
member.toggle(:deceased)

Why is irb returning NameError: uninitialized constant for all objects in the database?

I am half way through building a Q&A app and I have successfully seeded my database and can see that objects have been created both in my browser and in the server logs and previously in irb. Now when I went to try to inspect an object in irb, for every object I give it, irb returns "NameError: uninitialized constant". For example:
2.0.0-p481 :001 > user = User.find(1)
NameError: uninitialized constant User
from (irb):1
from /home/vagrant/.rvm/rubies/ruby-2.0.0-p481/bin/irb:12:in `<main>'
2.0.0-p481 :002 > question = Question.find(1)
NameError: uninitialized constant Question
from (irb):2
from /home/vagrant/.rvm/rubies/ruby-2.0.0-p481/bin/irb:12:in `<main>'
2.0.0-p481 :003 > user = User.find
NameError: uninitialized constant User
from (irb):3
from /home/vagrant/.rvm/rubies/ruby-2.0.0-p481/bin/irb:12:in `<main>'
2.0.0-p481 :004 > user = User.new
NameError: uninitialized constant User
from (irb):4
from /home/vagrant/.rvm/rubies/ruby-2.0.0-p481/bin/irb:12:in `<main>'
I'm new to programming, so if anyone could explain why this might be happening it would be much appreciated!
It looks like you're running irb when you want rails console (or rails c for short).
Because you need to use rails console, not irb.
rails console
(I assume you are building a Rails application)

Httparty in rails

I am trying to learn how to use httparty. I ran 'gem install httparty', first in my terminal, expecting to be able to use it in a pry session but no luck.
Next, I created a new rails app, added the gem to my gem file and ran bundle and in a pry session tried to use httparty as follows:
[1] pry(main)> HTTParty.get("http://rubygems.org/api/v1/versions/httparty.json")
NameError: uninitialized constant HTTParty
from (pry):1:in `__pry__'
[2] pry(main)> HTTParty.get("http://rubygems.org/api/v1/versions/httparty.json)
[2] pry(main)* httParty.get("http://rubygems.org/api/v1/versions/httparty.json)
SyntaxError: unexpected tIDENTIFIER, expecting ')'
httParty.get("http://rubygems.org/api/v1/versions/httparty.json)
^
[2] pry(main)> httParty.get("http://rubygems.org/api/v1/versions/httparty.json")
NameError: undefined local variable or method `httParty' for main:Object
from (pry):2:in `__pry__'
[3] pry(main)> response = httParty.get("http://rubygems.org/api/v1/versions/httparty.json")
NameError: undefined local variable or method `httParty' for main:Object
from (pry):3:in `__pry__'
[4] pry(main)> response = HTTParty.get('https://api.stackexchange.com/2.2/questions?site=stackoverflow')
NameError: uninitialized constant HTTParty
from (pry):4:in `__pry__'
Any help would be much appreciated. Thank you
require the gem, either in your pry invocation
pry -rhttparty
or once in pry
require 'httparty'
I am working on the exact same thing right now. What I did was create a file test_party.rb in the rails lib directory. (Make sure to add config.autoload_paths += %W(#{config.root}/lib) to your config/application.rb
In the new lib file create a class TestParty and include HTTParty
Then in rails console you can run TestParty.whatever_you_want
Hope that helps!

Resources