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

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

Related

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.

Ruby on Rails where_values method

In Ruby on Rails 4 there was where_values method:
User.with_all_credentials(creds).where_values
[
[0] "lower(email) = 'test'"
]
In Ruby on Rails 5 when I try to do the same it returns me that this method not exists. How can I get the same result in Ruby on Rails 5?
As it might be easily seen in the Rails code, it’s now where_clause.
(Because where lives now in CLAUSE_METHODS.)
i think this may help out:-
The replacement is either to use where_values_hash method or to introspect where_clause
https://apidock.com/rails/ActiveRecord/Relation/where_values_hash

Return Values for Ruby and Rails Match Up ... Unexpectedly?

In an introduction to Ruby classes and inheritance in Ruby on Rails Rails Tutorial, Michael Hartl asks why, when running the static_pages_controller.rb in the rails console, an action returns nil when it is called on a StaticPagesController object:
class StaticPagesController < ApplicationController
def home
end
...
end
In the rails console:
>> controller = StaticPagesController.new
=> #<StaticPagesController:0x22855d0> (*)
>> controller.home
=> nil
(*)(my rails c returns attributes associated with the object, but they do not show up in the 2nd edition, but I'm using Rails 4.0.2, and he's on Rails 3, I believe, which might be the answer to my question, but we'll see)
Specifically, he asks the following:
But wait--actions don't return values, at least not ones that matter. The point of the home action ... is to render a web page, not to return a value. ... What's going on?
His answer is just that Rails is not Ruby:
Rails is written in Ruby, but Rails isn't Ruby. Some Rails classes are used like ordinary Ruby objects, but some are just grist for the Rails' magic mill. [emphasis added]
My question is, why does that count as the answer? When I run the same commands in IRB (after having created my own StaticPagesController class -- albeit it does not inherit from Rails' ActionController), I also get a nil return value:
My-Computer:app_root_directory David$ rails c
2.0.0p353 :001 > controller = StaticPagesController.new
2.0.0p353 :009 > controller.home
=> nil
2.0.0p353 :010 > exit
My-Computer:app_root_directory David$ irb
cannot load such file -- hirb
>> class Controller
>> def home
>> end
>> end
=> nil
>> controller = Controller.new
=> #<Controller:0x007ff365299c70>
>> controller.home
=> nil
Basically, am I making a mountain out of a molehill by asking this question? Is it alright not understanding why he's making a point about why an action (which is really just a method of a controller class) returns nil when called in the Rails console?
I think you misinterpreted the question. If you look at a controller the way you look at an ordinary Ruby class, then the home method... makes no sense. It does nothing interesting (returning nil is kinda boring). As a Ruby programmer you wouldn't put it there, because Ruby programmer declares methods in order to use them (so they should return a value, or just do something observable)
In Rails on the other hand actions (even empty) are essential part of the MVC pattern. You declare those methods and never call them (at least not explicitly), because in Rails it's not just a method, it's a way of telling Rails what to do.
This is why he emphasizes that Rails is not Ruby, especially when you use it with gems like Capybara and Cocumber. They are Domain Specific Languages (DSLs). Yes, they use Ruby, but they are not Ruby.
In ruby everything returns an object. So the statement "actions don't return values" isn't technically true. Without the context of the rest what's said I'm guessing he means calling an action in the Rails request lifecycle doesn't return a value you care about. Somewhere in the Rails stack the call to the action returns an object, but it is irrelevant to your development.
As for your console output, you are only getting nil because your action is empty. If you changed the action definition to:
def home
1
end
Then you would receive 1 as your return object, both in the Rails context and outside of it. I think another key point is to consider nil a value, just one that represents the absence of a value.

Hash does not contain 'try' method

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

"puts" method in rails 3

I've been doing some development with rails 3 and I was wondering why calling the "puts" method doesn't output to standard out.
How do I get this back?
Instead of puts, use logger.info:
logger.info #collection.inspect
You can also use the standard ruby way by calling STDOUT << 'your output'. Method put is not a rails speciality, it comes with ruby. If you use rails, you can also rely on the logger object.

Resources