Ruby on Rails where_values method - ruby-on-rails

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

Related

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

Is Rails group_by deprecated?

I have an array that I want to group and it seems like the 'group_by' function is OK for my situation.
http://apidock.com/rails/Enumerable/group_by
I used it in Rails 3.2.13.
grouped_array = my_array.group_by(&:my_function)
# Assume run 'my_function' have result1 on element1, element3 and result2 on element2, element4, then:
# grouped_array = {
# result1 => [element1, element3],
# result2 => [element2, element4],
# ...
# }
But I see it's deprecated in 4.0.2.
Which function should I use?
It's not deprecated.
Ruby prior to 1.8.7 didn't have group_by builtin, so rails added it. Ruby 1.8.7 added group_by, but whereas the rails group_by returned ordered hashes, the ruby 1.8.7 returned plain hashes (since ordered hashes weren't yet in ruby 1.8.7), so rails continued to overwrite the method.
Ruby 1.9 did have group_by, so rails no longer needed to overwrite it, but this code was left there for people still running 1.8.7.
Rails 4 dropped ruby 1.8.7 compatibility, so code like this was removed from rails. It's not deprecated, but it's no longer in rails because it has become part of the ruby standard library.
Would Array#sort do the trick?
#my_array.sort { |item1, item2| item1.my_function <=> item2.my_function }

How to get all files in image directory Rails4 in array

I have migrated from Rails3 to Rails4. The following code returns Array in Rails3 but in Rails4 it returns string with illegal character.
Dir.glob("app/assets/images/flowers/*")
sample output in Rails3
["app/assets/images/flowers/rose.png", "app/assets/images/flowers/lilly.png"]
output in Rails4
"\x04\b[dI\"8app/assets/images/flowers/rose.png\x06:\x06ETI\"4app/assets/images/flowers/lilly.png"
How to get same output format as in Rails3?
try this
files = Dir.glob("app/assets/images/flowers/*").map do |f| File.basename f end
Dir has nothing to do with Rails — it's pure Ruby class. Here is the API reference to it. According to API it should always return an Array. My guess is that you messed up something in your Ruby installation while you were upgrading Rails 3 to 4.
I think best bet will be a clean installation of ruby/rails. You could also try to run Dir.glob() from both IRB and rails console to see where the mistake happens; and start from there.

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

In Ruby on Rails is there a way to view the properties of a model

I am using NetBeans to create my first Ruby on Rails application. Is there a way by which I can view all of the properties of a model? Should I just be looking in the database?
I just use db/schema.rb - seems to work fine for me.
You can use the annotate gem for this - it will add comments to the top of each model indicating its properties.
You could call Model.attributes in Rails' console. This gives a hash with all attributes.
Be careful when using this inside your real code as it always recreates the hash and is therefore pretty expensive.
Just type your model name on the rails console then press Enter.
rails c # start your rails console
User
=> User(id: integer, email: string, password_digest: string...)
In case anyone is viewing this and is using a newer version of Rails, you can just call Model in the console. (I'm on Rails 3.2.1 and Ruby 1.9.2p290.)
In fact, calling Model.attributes in this case does not work.
A nice little tool that I use is from MySQL. It is called MySQL Administrator.
It lets me validate that the db (development/test/production), the tables for the db, and finally the columns(attributes).
Model.attributes #=> {'name' => 'foo', 'desc' => 'bar'}
P.S. www.railsbrain.com - use this.

Resources