Rails 3.2 Obj.try() failing - ruby-on-rails

I just upgraded from Rails 3.1.0 to 3.2.3. Once I got my gem dependency issues cleared up I managed to run tests and I discovered this:
1.9.2p180 :005 > Rails.version
=> "3.2.3"
1.9.2p180 :006 > a = 'test'
=> "test"
1.9.2p180 :007 > a.try(:banana)
NoMethodError: undefined method `banana' for "test":String
from /Users/jamesthullbery/.rvm/gems/ruby-1.9.2-p180#app_tier/gems/activesupport-3.2.3/lib /active_support/core_ext/object/try.rb:32:in `try'
from (irb):7
from /Users/jamesthullbery/.rvm/gems/ruby-1.9.2-p180#app_tier/gems/railties-3.2.3/lib/rails /commands/console.rb:47:in `start'
from /Users/jamesthullbery/.rvm/gems/ruby-1.9.2-p180#app_tier/gems/railties-3.2.3/lib/rails/commands/console.rb:8:in `start'
from /Users/jamesthullbery/.rvm/gems/ruby-1.9.2-p180#app_tier/gems/railties-3.2.3/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
1.9.2p180 :008 >
When I perform the same command on Rails 3.1.0 I get no issues:
1.9.2p180 :004 > Rails.version
=> "3.1.0"
1.9.2p180 :005 > a = 'test'
=> "test"
1.9.2p180 :006 > a.try(:banana)
=> nil
1.9.2p180 :007 >
Has anyone seen this issue? Certainly there is something wrong with my environment and not the Rails framework. It seems to be pretty difficult to accurately search for the method try, so I haven't found anything online yet. Is anyone else seeing this? Thanks!

Object#try is supposed to deal with nil receivers. It should not be used to avoid NoMethodError exceptions.
The behavior you saw in 3.1.0 was a bug and it was "fixed" 7 months ago (as of this writing).

Related

NameError: uninitialized constant DeviseMultipleTokenAuthDevice

Hello Programmers & Developers!!!, I'm facing a problem in rails console when I tried to access DeviseMultipleTokenAuthDevice then I'm getting the following error :
Loading development environment (Rails 4.2.0)
2.2.4 :001 > DeviseMultipleTokenAuthDevice
NameError: uninitialized constant DeviseMultipleTokenAuthDevice
from (irb):1
from /Users/vishal/.rvm/gems/ruby-2.2.4#devise_demo/gems/railties-4.2.0/lib/rails/commands/console.rb:110:in `start'
from /Users/vishal/.rvm/gems/ruby-2.2.4#devise_demo/gems/railties-4.2.0/lib/rails/commands/console.rb:9:in `start'
from /Users/vishal/.rvm/gems/ruby-2.2.4#devise_demo/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:68:in `console'
from /Users/vishal/.rvm/gems/ruby-2.2.4#devise_demo/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /Users/vishal/.rvm/gems/ruby-2.2.4#devise_demo/gems/railties-4.2.0/lib/rails/commands.rb:17:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
2.2.4 :002 >
Following gems I'm using in my project for user authentication
gem 'devise'
gem 'devise_multiple_token_auth'
Following are the tables in my record :
2.2.4 :002 > p ActiveRecord::Base.connection.tables
["schema_migrations", "users", "devise_multiple_token_auth_devices"]
=> ["schema_migrations", "users", "devise_multiple_token_auth_devices"]
2.2.4 :003 >
For more info you can follow these links :
Source from where I followed the instruction
My Github repo link for project source
Other required information
Ruby Version 2.2.4
Rails Version 4.2.0
Database PSQL
The solution for the same is, we've to create a file called devise_multiple_token_auth_device.rb in app/models/
here is the code for devise_multiple_token_auth_device.rb file
class DeviseMultipleTokenAuthDevice < ActiveRecord::Base
belongs_to :user
end

find_all_by_{attribute} doesn't work for activerecord 4.1.5

I'm using find_all_by to get the returned array from active record, and this feature works well for Rails(4.0.0)
Loading production environment (Rails 4.0.0)
2.0.0-p353 :001 > Subscription.find_all_by_id 123
=> []
But when I changed it to Rails(4.1.5), find_all_by doesn't work, and throw undefined method error
Loading production environment (Rails 4.1.5)
2.0.0-p481 :001 > Subscription.find_all_by_id 123
NoMethodError: undefined method `find_all_by_id' for #<Class:0x00000004d71b68>
from /usr/local/rvm/gems/ruby-2.0.0-p481/gems/activerecord-4.1.5/lib/active_record/dynamic_matchers.rb:26:in `method_missing'
from (irb):1
from /usr/local/rvm/gems/ruby-2.0.0-p481/gems/railties-4.1.5/lib/rails/commands/console.rb:90:in `start'
from /usr/local/rvm/gems/ruby-2.0.0-p481/gems/railties-4.1.5/lib/rails/commands/console.rb:9:in `start'
from /usr/local/rvm/gems/ruby-2.0.0-p481/gems/railties-4.1.5/lib/rails/commands/commands_tasks.rb:69:in `console'
from /usr/local/rvm/gems/ruby-2.0.0-p481/gems/railties-4.1.5/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
from /usr/local/rvm/gems/ruby-2.0.0-p481/gems/railties-4.1.5/lib/rails/commands.rb:17:in `<top (required)>'
Any ideas to fix it or work around it?
How about this?
Subscription.where(id: "123").to_a
For anyone else who has been trying to get find_all_by to work and have had no success, find_all_by has been deprecated and removed from rails.
Source: https://github.com/rails/rails/issues/17357

Getting a lot of extra error information ".../commands/console.rb:..."

I'm working on a ruby on rails tutorial and using the rails console
When an error occurs I'm seeing a lot more error information than the tutorial shows (could be that they ommited it for brevity)
For example:
2.1.1 :001 > asdf
NameError: undefined local variable or method `asdf' for main:Object
from (irb):1
from /Users/me/.rvm/gems/ruby-2.1.1#global/gems/railties-4.0.4/lib/rails/commands/console.rb:90:in `start'
from /Users/me/.rvm/gems/ruby-2.1.1#global/gems/railties-4.0.4/lib/rails/commands/console.rb:9:in `start'
from /Users/me/.rvm/gems/ruby-2.1.1#global/gems/railties-4.0.4/lib/rails/commands.rb:62:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
2.1.1 :002 >
Whereas the tutorial would only show:
2.1.1 :001 > asdf
NameError: undefined local variable or method `asdf' for main:Object
Is that other information useful? Is there a way to suppress it?

Ruby on Rails SNMP gem - no response from device

I'm using the SNMP gem in a Rails app to control a Cisco switch. The "get" features of the Gem work, and I can retrieve values from the devices, but the "set" feature isn't working so well...
From the command line, the SNMP commands work properly:
bash-4.1# snmpset -v2c -c private-string 192.168.3.1 .1.3.6.1.2.1.2.2.1.7.11 i 2
IF-MIB::ifAdminStatus.11 = INTEGER: down(2)
bash-4.1#
But when I try to set the same OID using the Gem, I get "no response from 192.168.3.1"
In both the actual app and in IRB:
bash-4.1# irb
2.0.0-p0 :001 > require 'snmp'
=> true
2.0.0-p0 :002 > include SNMP
=> Object
2.0.0-p0 :003 > SNMP::Manager.open(:host => "192.168.3.1", :community => "private-string") do |manager|
2.0.0-p0 :004 > varbind = VarBind.new("1.3.6.1.2.1.2.2.1.7.11", "2")
2.0.0-p0 :005?> manager.set(varbind)
2.0.0-p0 :006?> end
SNMP::RequestTimeout: host 192.168.3.1 not responding
from /usr/local/rvm/gems/ruby-2.0.0-p0/gems/snmp-1.1.1/lib/snmp/manager.rb:293:in `set'
from (irb):5:in `block in irb_binding'
from /usr/local/rvm/gems/ruby-2.0.0-p0/gems/snmp-1.1.1/lib/snmp/manager.rb:205:in `open'
from (irb):3
from /usr/local/rvm/rubies/ruby-2.0.0-p0/bin/irb:16:in `<main>'
2.0.0-p0 :007 > exit
I can run the snmpset from the command line on the same machine, and it works correctly, but not using the gem.
Try varbind = VarBind.new("1.3.6.1.2.1.2.2.1.7.11", SNMP::Integer.new(2)) so that the varbind type is correct.

How to do a simple `Date.today` in my Rails console

When I execute the command Date.today in my rails command, I get the following error:
I can't do a simple Date.today in my Rails console
ruby-1.9.2-p180 :026 > >> require 'date'
=> false
ruby-1.9.2-p180 :027 > >> Date.today
ArgumentError: invalid date
from /Users/besi/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/date.rb:809:in `civil'
from /Users/besi/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/Date.rb:1810:in `today'
from (irb):27
from /Users/besi/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.2.11/lib/rails/commands/console.rb:47:in `start'
from /Users/besi/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.2.11/lib/rails/commands/console.rb:8:in `start'
from /Users/besi/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.2.11/lib/rails/commands.rb:41:in `<top (required)>'
from /Users/besi/Dropbox/projects/Freelance/vidis/VidiServer/script/rails:6:in `require'
from /Users/besi/Dropbox/projects/Freelance/vidis/VidiServer/script/rails:6:in `<top (required)>'
from -e:1:in `load'
from -e:1:in `<main>'
In the irb console this works without problems:
ruby-1.9.2-p180 :001 > require 'date'
=> true
ruby-1.9.2-p180 :002 > Date.today
=> #<Date: 2013-04-15 (4912795/2,0,2299161)>
It looks like there is a problem with the RubyMine Plugin here. When I do the same thing in the console it works. I don't know though why such a simple thing should not work in RubyMine.

Resources