Why does sort_by change my array elements? - ruby-on-rails

I would expect that application of sort_by would change the order, but nothing else, about an array.
Here's my original line:
gon.frames = #job.sequence.frames.sort_by(&:n)
And a modified line, where sort_by is removed:
gon.frames = #job.sequence.frames
I'm finding that the values in gon.frames (which is transmitted to the browser using the gon gem) are different, not just in order, but in how they are converted to strings.
This issue seemed to appear after upgrading to ruby 3.0.0.
Edit: inspecting with Rails console shows that the first line is returning type Array, whereas the second line is returning type ActiveRecord::Associations::CollectionProxy.
I am still confused, because this didn't seem to be happening prior to my recent upgrades and change to ruby 3.0.0.
Edit - not sure what the root cause is here, but I rolled back to ruby 2.6.1 and the problem went away.
Edit - I think the change that I experienced may not be related to the type of frames. Instead, it could be that gon is doing something differently in how it passes this variable to the browser when ruby 3.0.0 is used.

Related

Randomly lost data on activeadmin

I have a weird problem with activeadmin. Randomly and unexpectedly some data on a resource not showed (like it has benn empty in DB). When I check the DB, all data it is on db and it is correct.
Also the data when take some .json resource is empty too.
The only solution I found for this problem is restart nginx, but it is a problem because the client not see the information on the system.
I check the logs, but I do not see any relevant information. Is there any way to get information about it for solve this kind of problems?
System versions:
Rails 5.1.3
ruby 2.2.5p319 (2016-04-26 revision 54774) [x86_64-linux]
nginx/1.10.3 Phusion_Passenger/5.1.4
Finally I found the problem but I do not understand what is wrong on it....
This are the involucrated files:
https://gist.github.com/cpfarher/bfde79dd9c3772575b03712c0a397110
The problem occured when you open the route: lines/1/edit action. After open it, data on https://gist.github.com/cpfarher/bfde79dd9c3772575b03712c0a397110#file-admin_recipe-rb is not showed.
If you change de sentence "select distinct" on line:
https://gist.github.com/cpfarher/bfde79dd9c3772575b03712c0a397110#file-recipe_fail-rb-L10
all works fine, the field on https://gist.github.com/cpfarher/bfde79dd9c3772575b03712c0a397110#file-admin_recipe-rb-L13 showed ok. But, if you use the clause with select distinct, the field it is not showed....
On the other hand if you use the file: https://gist.github.com/cpfarher/bfde79dd9c3772575b03712c0a397110#file-recipe_ok-rb instead of https://gist.github.com/cpfarher/bfde79dd9c3772575b03712c0a397110#file-recipe_fail-rb all works fine.... :|

Updating a Rails model's attributes through mongoid using normal persistence methods

I have been chasing an issue down for a while now, and still cannot figure out what's happening. I am unable to edit documents made from my gem through normal persistence methods, like update or even just editing attributes and calling save.
For example, calling:
Scram::Policy.where(id: a.id).first.update!(priority: 12345)
Will not work at all (there are no errors, but the document has not updated). But the following will work fine:
Scram::Policy.collection.find( { "_id" => a.id } ).update_one( { "$set" => {"priority" => 12345}})
I am not sure what I'm doing wrong. Calling update and save on any other model works fine. The document in question is from my gem: https://github.com/skreem/scram/blob/master/lib/scram/app/models/policy.rb
I cannot edit its embedded documents either (targets). I have tried removing the store_in macro, and specifying exactly what class to use using inverse_of and class_name in a fake app to reimplement these classes: https://github.com/skreem/scram-implementation/blob/master/lib/scram/lib/scram/app/models/policy.rb
I've tried reimplementing the entire gem into a clean fake rails application: https://github.com/skreem/scram-implementation
Running these in rails console demonstrates how updating does not work:
https://gist.github.com/skreem/c70f9ddcc269e78015dd31c92917fafa
Is this an issue with mongoid concerning embedded documents, or is there some small intricacy I am missing in my code?
EDIT:
The issue continues if you run irb from the root of my gem (scram) and then run the following:
require "scram.rb"
Mongoid.load!('./spec/config/mongoid.yml', :test)
Scram::Policy.first.update!(priority: 32) #=> doesn't update the document at all
Scram::Policy.where(id: "58af256f366a3536f0d54a61").update(priority: 322) #=> works just fine
Oddly enough, the following doesn't work:
Scram::Policy.where(id: "58af256f366a3536f0d54a61").first.update(priority: 322)
It seems like first isn't retrieving what I want. Doing an equality comparison shows that the first document is equal to the first returned by the where query.
Well. As it turns out, you cannot call a field collection_name or else mongoid will ensure bad things happen to you. Just renaming the field solved all my issues. Here's the code within mongoid that was responsible for the collision: https://github.com/mongodb/mongoid/blob/master/lib/mongoid/persistence_context.rb#L82
Here's the commit within my gem that fixed my issue: https://github.com/skreem/scram/commit/25995e955c235b24ac86d389dca59996fc60d822
Edit:
Make sure to update your Mongoid version if you have dealt with this issue and did not get any warnings! After creating an issue on the mongoid issue tracker, PersistenceContext was added to a list of prohibited methods. Now, attempting to use collection_name or collection as a field will cause mongoid to spit out a couple of warnings.
Fix commit: https://github.com/mongodb/mongoid/commit/6831518193321d2cb1642512432a19ec91f4b56d

Ruby Rubocop. MutableConstant - not detecting freeze

I wrote a lib/animal.rb with several lists of params, and I want to reference that list in my controller and add it my params list. I did this because I use this list in several locations and didn't want to litter my code with a bunch of references to the library.
Controller
ANIMAL_TYPE_INPUT_PARAMS = *Animals::ANIMAL_TYPE_PARAMS.freeze
....
def familar_params
params.permit(ANIMAL_TYPE_INPUT_PARAMS, OTHER_PARAM_LIST....)
end
Lib/animal.rb
module Animal
# param lists
ANIMAL_TYPE_PARAMS = [
:animal_has_fur, :animal_id, :animal_weight
].freeze
end
Functionally it works just fine, but I am seeing a weird rubocop error. I would prefer to not disable MutableConstant for this section (disabling rubocop is usually a band aid that you pay for at some point).
Rubocop error
app/controllers/api/v1/example_controller.rb:55:24: C: Freeze mutable objects assigned to constants.
ANIMAL_TYPE_INPUT_PARAMS = *Animals::ANIMAL_TYPE_PARAMS.freeze
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
I looked into this question: Ruby rubocop: how to freeze an array constant generated with splat But mine are already arrays, so I feel like it doesn't apply to me / shouldn't have to call to_a.
As #drenmi suggested, it was an older version of rubocop giving me this error. Once I upgraded to 0.46.0 the error was no longer.

Using Rails4.1.1 and Ruby 2.0.0, binding.pry's terminal output is sometimes empty

I'm trying to debug a Rails 4.1.1 app using binding.pry but more often than not when the execution breaks and I try to show the values of my variables it just returns an empty string, or nothing (hard to distinguish).
But other times it spits out the values. I've noticed that it's more likely to spit out the value of a variable if that variable is holding a very long piece of text or JSON. Otherwise it's just ignored.
Likewise trying to do other basic binding.pry things like ! to reset the input buffer or hist --tail 5 etc are just ignored.
Rails is being started via foreman and running in thin.
My guess is it's some sort of output synchronisation issue but that's just a guess.
Have you experienced this and, if so how did you deal with it?
Are you using pry-debugger? If so, with Ruby 2+, you may want to change to pry-byebug. There is some kind of linecache incompatibility between Ruby 1.9 & 2.x, and byebug fixes that.

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.

Resources