Hash does not contain 'try' method - ruby-on-rails

I am noticing differences between a hash object within Ruby 1.8.7 and a hash object within Rails 3.0.10.
For example, within the 1.8.7 irb, I get:
1.8.7 :001 > {}.try(:method)
NoMethodError: undefned method `try' for {}:Hash
from (irb):1```
However, from the 3.0.10 rails console, I get:
1.8.7 :003 > {}.try(:method_x)
NoMethodError: undefined method `method_x' for {}:Hash
from (irb):3:in `try'
from (irb):3
This surprises me because I was under the impression that try is defined in Object which is an ancestor of Hash and try will return nil instead of throwing a NoMethodError.
What am I missing?

This surprises me because I was under the impression that try is defined in Object which is an ancestor of Hash and try will return nil instead of throwing a NoMethodError.
What am I missing?
Your impression of which class try is defined in is correct (Object). What you are missing is what file it is defined in. It's defined in the ActiveSupport library, not in the Ruby core library.
So, you need to
require 'active_support/core_ext/object/try'
first.

try is not part of ruby 1.8.7, though Rails does include it through ActiveSupport. try is part of Object from ruby 1.9+ (afaik).

Related

How can I find a definition true_user for Rails app using devise gem?

After Rails 5.2 and ruby 2.6.5 upgrade, I am getting errors: (1) NameError: uninitialized constant UserUploader::Uploader and (2) ArgumentError undefined class/module UserUploader::Uploader when calling true_user within app/controllers/application_controller.rb.
Where can I find a definition of method true_user (I couldn't find any source for this method in the web)?
I am not seeing the relationship with true_user call and class UserUploader < ImageUploader.

ActiveRecord::Relation.concat failing in Rails 5

Stupid question but I'm not sure why this would work in Rails 4.2 but not in Rails 5.2.
FamilyCharacteristic.where(family_id: #user.family_ids)
.concat(#user.characteristics)
Specs fail in 5.2:
Failure/Error:
FamilyCharacteristic.where(family_id: #user.family_ids)
.concat(#user.characteristics)
NoMethodError:
undefined method `concat' for #<ActiveRecord::Relation []>
Did you mean? count
Was concat removed from ActiveRecord::Relation in 5.2 or was FamilyCharacteristic.where(family_id: #user.family_ids) somehow a different object in < 4.2?
Thanks for any help.
I did some digging and found out that:
FamilyCharacteristic.where(family_id: #user.family_ids)'s class didn't change, it's still ActiveRecord::Relation
Relation didn't and still doesn't define its own concat method, but it was delegated to Array#concat until this commit happened, so in Rails 4.2 SomeModel.where(id: ids).concat(some_records)(which returns an Array) was actually the same as SomeModel.where(id: ids).to_a.concat(some_models)
After mentioned before change in ActiveRecord::Delegation, in Rails 5.2, the only methods delegated to Array are the ones specified in this module and concat is not among them
To sum up - concat from your example was never part of ActiveRecord but was delegated to Array#concat and that's why it worked. It's no longer delegated in Rails 5 so it throws NoMethodError.

Why can't I use the pluralize method in rails console? [duplicate]

This question already has an answer here:
undefined method pluralize for main:Object
(1 answer)
Closed 5 years ago.
I have just come to know that I can't use the method 'pluralize' in the rails console or IRB. Is there anything I don't understand about this?
2.3.0 :001 > pluralize
NameError: undefined local variable or method `pluralize' for main:Object
It gets interpreted well when it's used in the ruby or view file. Why can't I use it in the rails console?
The pluralize method used in Rails views is defined in ActionView::Helpers::TextHelper. To use it in rails console you need to include it
$ rails console
2.3.3 :008 > include ActionView::Helpers::TextHelper
2.3.3 :009 > pluralize 2, 'man'
=> "2 men"
or call them through the helper variable
$ rails console
2.3.3 :0010 > helper.pluralize(2, 'man')
=> "2 men"
It should become clear by looking at the documentation: http://apidock.com/rails/ActionView/Helpers/TextHelper/pluralize
pluralize is defined on TextHelper, which means that it is available to your helps and views through ActionView.
You can however use it in rails console like this:
ActionController::Base.helpers.pluralize(...)
Or by including TextHelper:
include ActionView::Helpers::TextHelper
It gets interpreted well when it's used in the ruby or view file. Why can't I use it in the rails console?
Because it was meant to be used from views, not from console (by being defined as an action view helper).
But not all hope is lost. You can access helper methods in console!
helper.pluralize(...)

Adding nonce ruby

Ruby nonce throwing error
require 'date'
nonce = DateTime.now.to_i
Error:
undefined method `to_i' for #<DateTime:0x000000015336e8> (NoMethodError)
Working in my console it gives correct value
2.1.0 :014 > nonce = DateTime.now.to_i
=> 1405065242
Why it throws error programatically?
EDIT
Is there any way to add nonce. The condition is it should be a integer that has to be incremented on every subsequent request
As Pavan sais, if you run your code in irb it probably will not working anymore.
According to the doc, Ruby hasn't method to_i in DateTime class.
However, Rails override DateTime class to have a to_i method:
So, I think you run the command whick works in an Ruby On Rails environment, that's why it works. But if you run in a Ruby environment without Rails, it will not work.
Hope it helps.

.to_date method failing

I am using rails 3.2.16 and ruby 1.9.3
I get this error from the localhost browser after rails s.
undefined method `to_date' for nil:NilClass
like this
NoMethodError in Products#new
Showing /Users/main/railscasts-episodes/episode-350/store-
after/app/views/products/_form.html.erb where line #27 raised:
undefined method `to_date' for nil:NilClass
in irb> mode it works only when I
require 'date'
Now my question is about my rails project. In which file in my project I should add
require 'date'
I added it into my model it did not work. Or, maybe I need to install any specific gem? to make this work
The problem you have isn't an issue with to_date, it's a problem with the object you're trying to perform the method on
Your issue will either be:
You have not declared the variable / object
Your variable / object is not populated with any data
I'd highly recommend posting the controller action you're using to invoke the to_date method. Obviously, there may be an issue with the way you're calling it, but the immediate issue is you're trying to deal with an unpopulated variable

Resources