Is Rails group_by deprecated? - ruby-on-rails

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 }

Related

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

Call to where function on model with a hash only doesn't find DB record

I am trying to retrieve a record using Rails .where() function on an ActiveRecord model.
The function will only return a record if the clause is defined in a string, so:
Returns single record as expected
Model.where("business_id = 16")
Model.where("business_id = :business_id", {business_id: 16})
(for which I see the SQL: WHERE (business_id = 16))
Returns empty array
Model.where({:business_id => 16})
(for which I see WHERE model.business_id = 16)
Model.where({business_id: 16})
Model.where(:business_id => 16)
The database record exists and the field we are querying on is defined as an integer there.
What is going on?
Tests run from a binding.pry
Version Data
Rails 3.2.21
ruby 2.1.5p273 (2014-11-13 revision 48405) [x86_64-darwin13.0]
Pry version 0.10.1 on Ruby 2.1.5
Whatever you have written is perfectly fine. You just please run the following line on rails console.
Model.find_by_business_id(16)
I think it will return nothing. There is no record with business_id = 16
Whenever you use the where() and pass the arguments, it will take those as a hash(key-value pair). So you don't need to pass like this
Model.where({:business_id => 16})
You should pass something like this
in rails 3
Model.where(:business_id => 16)
in rails 4
Model.where(business_id: 16)
Also you can use rails 3 syntax for where clause in rails 4 also.

Why does the Rails.cache.write return a different value in Rails 2.3.11 and Rails 3.2.11 console?

I am trying to expire key in 10 seconds, somehow it is, but not working with rspec. In this process, I noticed Rails.cache.write return false in Rails 2.3.11, while Rails.cache.write return true in Rails 3.2.11, is this a problem? why different value? Why Rails 2.3.11 return false and Rails 3.2.11 return true?
Rails 2.3.11
irb(main):001:0> Rails.cache.write("test", "java", :expires_in => 10.seconds)
=> false
Rails 3.2.11
irb(main):001:0> Rails.cache.write("test", "java", :expires_in => 10.seconds)
=> true
I am using jruby 1.6.5.1 with Rails 2.3.11 and jruby 1.7.3 with Rails 3.2.11.
The Rails.cache.write method is what is sometimes known as a command method, which is called for its side effects, as opposed to a query method, called for its return value (for more info, check out command-query separation).
Since the Rails docs make no guarantees about the return value, it is probably best not to depend on it, since it might (and apparently has) change without warning.

Ruby on Rails 3.1 : unexpected to_json behaviour with arrays

I have the following structure :
a = { 'x' => [1,2,3] }
In Rails 3.0 with ruby 1.8, the conversion to JSON works as expected :
a.to_json
=> "{\"x\":[1,2,3]}"
In Rails 3.1 with ruby 1.9.3p125, the array is transformed into a hash :
a.to_json
=> "{\"x\":{\"1\":null,\"2\":null,\"3\":null}}"
I can't find any documentation explaining the difference in behaviour between the two versions, nor any way to preserve the expected output in Rails 3.1. Any clues ?
The problem is actually related to one of the gems installed on the Gemfile, which returns an incorrect result for to_json calls.
Thanks for the help.

Does Rails 2.3 and Rails 3.0 handle the displaying of arrays differently?

I am going through a video tutorial that was using Rails 2.3, and they did:
<%= first_array = ['a', 'b', 'c'] %>
When they did that, the output they got was:
abc
When I am trying to follow along, on my setup (Rails 3.0), I get:
["a", "b", "c"]
Is this difference normal or did I do something incorrectly?
Thanks.
Rails doesn't display arrays differently between 2 and 3. But between ruby 1.8 and 1.9 it has changed.
In ruby 1.8 to_s returns self.join which joins all of the elements together without a separator.
In ruby 1.9 to_s is an alias for inspect which returns the array as "[#{self.join(', ')}]" (roughty speaking).
I don't know for sure, but I would guess this is a difference between versions of ruby rather than differences between versions of rails.

Resources