Ruby on Rails 3.1 : unexpected to_json behaviour with arrays - ruby-on-rails

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.

Related

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.

Rails 2.3.16 upgrade breaks ActiveSupport::JSON.decode

When i am trying to upgrade Rails to 2.3.16 from 2.3.15, the ActiveSupport::JSON.decode(response.body) fails to handle NaN.
Am getting an error like: invalid character at "NaN,...
Does anyone know how to fix it?
ActiveSupport::JSON.decode uses MultiJson under the hood which tends to ignore any passed in options (At least in Rails 3, not sure about 2.3). This means you can't pass in the usually accepted option of allow_nan.
However, if you are using Ruby 1.9+ you can use the built-in JSON parser:
require 'json'
json_result = JSON.parse(response.body, allow_nan: true)

rails array output strange format in production

anyone seen this array error before?
I have a helper method that returns an array. In development mode on my laptop it returns the array in an expected format:
var fire =
[[1349083353000, 8.860000000000582], [1349085153000, 19.779999999999745],
[1349086953000, 20.289999999999964], [1349088753000, 29.850000000000364],
[1349090553000, 3.7999999999992724]];
BUT same code in production returns a strange array format:
var fire = 135175422800015.5135175602800020.0135175782800018.99135175962800012.33135176142800019.13135176322800029.55135176502800020.13135176682800077.34
I have tried checking the output in rails console on either machine and the production output the same weird array format. I have created a new array from within rails console on production and it works as expected to output the correct format of array.
Anyone seen this bit of weirdness?
Rails version:3.2.8
Ruby Version:1.9.3p-125
You're probably developing on Ruby 1.9 and deploying on Ruby 1.8. The default behaviors for treating arrays are different.
In Ruby 1.8 array.to_s is equivalent to array.join('').
In Ruby 1.9 array.to_s is equivalent to array.inspect.
If you want the proper behavior on both, and you're using JavaScript, you might want to render it as JSON using array.to_json instead.

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