How do I use the Rails toggle? - ruby-on-rails

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)

Related

Pry-rails not reloading correctly

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

rails config/environments where is 'config' defined?

In config/environments/production.rb (and the other environments) there is:
config.eager_load = true
and a whole bunch of other config.foobar calls. But where does 'config' come from? Usually you have something like:
SomeClass.each do | block_variable |
block_variable.some_method
end
but in the case of the mystical 'config' this is not the case, its like a block variable which is not declared anywhere. Opening rails console, when I do:
irb(main):001:0> config
NameError: undefined local variable or method `config' for main:Object
Did you mean? conf
from (irb):1
irb(main):002:0>
and the same result occurs for app.config
irb(main):002:0> app.config
NoMethodError: undefined method `config' for #<ActionDispatch::Integration::Session:0x00007fc898d02808>
from (irb):2
How is it possible for ruby to allow calls on 'config'?
These classes include the ActiveSupport::Configurable module:
https://api.rubyonrails.org/classes/ActiveSupport/Configurable.html
Here is an alternative to implementing this behavior by yourself:
https://robots.thoughtbot.com/mygem-configure-block

undefined method pluralize for main:Object

I'm trying to test a method in my console, but even the basic pluralize -
pluralize(1, 'person')
wont work..
Output:
NoMethodError: undefined method 'pluralize' for main:Object
from (pry):42:in '<main>'
but helper.method(:pluralize) shows me : Method: ActionView::Base(ActionView::Helpers::TextHelper)#pluralize
What am i missing?
The helpers aren't included by default in the console. You can include them first and it'll work:
>> include ActionView::Helpers::TextHelper
>> pluralize(1, 'person')
# => "1 person"
Or, you can use the helper object which Rails gives you in the console:
>> helper.pluralize(1, 'person')
# => "1 person"

undefined method `isolation_level' for ActiveRecord::Base:Class

Where does the call to the isolation_level come from?
My module fails at a.save!
module AppsHelpers
def self.create_app!
a = App.new
a.save!
a
end
end
The specific NoMethodError:
Failure/Error: #app = AppsHelpers::create_app!
NoMethodError:
undefined method `isolation_level' for ActiveRecord::Base:Class
What could possibly cause this failure?
System:
ruby 1.9.3p448
Rails 3.2.8
This was simple. I was calling a method defined in the Transactional Isolation gem: The stacktrace was not helpful because I was calling it from an installed gem and this was a missing dependency.

NameError: undefined local variable or method `params' for main:Object

I'm using carrier wave and i get
NameError: undefined local variable or method `params' for main:Object
when i do u.commentfile = params[:file]
in the console? help!
The error is telling you exactly what's wrong. In the console, you're not in the scope of a request, so the params variable isn't defined unless you've done so manually. What are you trying to do?

Resources