ActiveRecord query fails in binding.pry - ruby-on-rails

I'm using binding.pry in a test environment but getting some very peculiar results:
[4] pry(#<RentalItem>)> self.charged_amounts
=> [#<ChargedAmount id: 1, type_of_amount:"rental", charge_id:1, rental_item_id:1>, #<ChargedAmount id: 2, ...>]
[5] pry(#<RentalItem>)> self.charged_amounts.where(type_of_amount:"rental")
=> []
[6] pry(#<RentalItem>)> self.charged_amounts.where(charge_id:1)
=> []
[7] pry(#<RentalItem>)> self.charged_amounts.where(rental_item_id:1)
=> []
Trying to understand and get some ideas in terms of why the behavior above might be happening. I.e., give me a starting point to debug.
Thanks

That's because of how rails handles transactional fixtures. Basically, records are deleted between tests. You can read more about it here Transactional Fixtures in Rails

Related

Ruby FrozenString error in Sidekiq Worker

I am using Ruby on Rails and trying to use a Sidekiq worker, but at some point I'm running into an issue where the worker calls a view, the view calls a concern, and then the concern isn't able to update a variable in its function because of the FrozenString error.
For example, here's how my worker looks:
class ReportGeneratorWorker
include Sidekiq::Worker, ReportHelper
sidekiq_options queue: Rails.env.to_sym
def perform
ac_base = ApplicationController.new
body_html = ac_base.render_to_string template: "common/report_templates/generate_pdf.html.erb", layout: false
end
end
Again, the view inserts text that leverages a concern, but the concern doesn't allow it to update. See below for example:
[3] pry(#<#<Class:0x00007fec8ceea400>>)> html
=> "<ul>"
[4] pry(#<#<Class:0x00007fec8ceea400>>)> html.class.name
=> "String"
[5] pry(#<#<Class:0x00007fec8ceea400>>)> html << "Hello"
FrozenError: can't modify frozen String
from (pry):5:in `replacement_text'
Any idea why this is happening? If I define the variable again from the Pry console, then it actually works:
[1] pry(#<#<Class:0x0000557f07a25130>>)> html
=> "<ul>"
[2] pry(#<#<Class:0x0000557f07a25130>>)> html << "TEST"
FrozenError: can't modify frozen String
from (pry):2:in `replacement_text'
[3] pry(#<#<Class:0x0000557f07a25130>>)> html = "<ul>"
=> "<ul>"
[4] pry(#<#<Class:0x0000557f07a25130>>)> html << "TEST"
=> "<ul>TEST"
[5] pry(#<#<Class:0x0000557f07a25130>>)>
I was able to resolve this issue by replacing << with +=.

Rails Digest::UUID v5 (vs) Postgresql uuid-ossp v5

I'm getting different V5 UUIDs when generating with Rails Digest::UUID and Postgresql uuid-ossp.
Rails:
[58] pry(main)> Digest::UUID.uuid_v5('e90bf6ab-f698-4faa-9d0f-810917dea53a', 'e90bf6ab-f698-4faa-9d0f-810917dea53a')
=> "db68e7ad-332a-57a7-9638-a507f76ded93"
Postgresql uuid-ossp:
select uuid_generate_v5('e90bf6ab-f698-4faa-9d0f-810917dea53a', 'e90bf6ab-f698-4faa-9d0f-810917dea53a');
uuid_generate_v5
--------------------------------------
6c569b95-a6fe-5553-a6f5-cd871ab30178
What would be the reason? I thought both should generate the same UUID when the input is the same, but it is different!
It's not an answer to the question about why Rails produces a different result, but if you want to produce v5 UUID in your Ruby code, you could use uuidtools. It returns the same result as PSQL:
~ pry
[1] pry(main)> require 'uuidtools'
=> true
[2] pry(main)> UUIDTools::UUID.sha1_create(UUIDTools::UUID.parse('e90bf6ab-f698-4faa-9d0f-810917dea53a'), 'e90bf6ab-f698-4faa-9d0f-810917dea53a')
=> #<UUID:0x3fe09ea60dd8 UUID:6c569b95-a6fe-5553-a6f5-cd871ab30178>
[3] pry(main)>
It seems that a patch is proposed so that working string-representation of namespaces can be enabled explicitly
The new behavior will be enabled by setting the config.active_support.use_rfc4122_namespaced_uuids option to
true.
but, the patch is very recent and it could be still under test. People can be afraid it breaks things. Check
https://github.com/rails/rails/issues/37681
https://github.com/rails/rails/pull/37682/files
Meanwhile, a workaround is to pack the namespace string
ns=n.scan(/(\h{8})-(\h{4})-(\h{4})-(\h{4})-(\h{4})(\h{8})/).flatten.map { |s| s.to_i(16) }.pack("NnnnnN")
In your example
irb(main):037:0> n='e90bf6ab-f698-4faa-9d0f-810917dea53a'
=> "e90bf6ab-f698-4faa-9d0f-810917dea53a"
irb(main):038:0> ns=n.scan(/(\h{8})-(\h{4})-(\h{4})-(\h{4})-(\h{4})(\h{8})/).flatten.map { |s| s.to_i(16) }.pack("NnnnnN")
=> "\xE9\v\xF6\xAB\xF6\x98O\xAA\x9D\x0F\x81\t\x17\xDE\xA5:"
irb(main):039:0> puts Digest::UUID.uuid_v5(ns, 'e90bf6ab-f698-4faa-9d0f-810917dea53a')
6c569b95-a6fe-5553-a6f5-cd871ab30178

Rails numericality validation is failing with a Fixnum value

I'm using Ruby 2.2.3 with Rails 4.2.8 and Oracle DB. I have a model called Historico, which has the following validation:
validates :anosemestre, presence: true, numericality: { only_integer: true }
Also, in my schema, I have the correct declaration for the anosemestre column:
t.integer 'anosemestre'
I'm trying to clone an old Historico to a new one by doing somethings like (historico_old is fresh from the DB):
attributes = historico_old.attributes
attributes.delete('id')
historico_novo = Historico.new(attributes)
binding.pry # for debug, obviously
(...)
historico_novo.save
But when I try to save it, the validation fails saying that anosemestre is not a number. When I debug it, I get the following output from pry, which made me even more confused:
[1] pry(Historico)> historico_novo.valid?
=> false
[2] pry(Historico)> historico_novo.errors.messages
=> {:anosemestre=>["is not a number"]}
[3] pry(Historico)> historico_novo.anosemestre
=> 20171
[4] pry(Historico)> historico_novo.anosemestre.class
=> Fixnum
[5] pry(Historico)> historico_novo.anosemestre = historico_novo.anosemestre.to_i
=> 20171
[6] pry(Historico)> historico_novo.anosemestre.class
=> Fixnum
[7] pry(Historico)> historico_novo.valid?
=> true
This issue did not occur in any other model that we have (and we have plenty), and is only happening when I copy the attributes from one historico to another. I have absoloutely no idea how to proceed from here, has anyone else experienced this issue before?

'last' method not returning last item in array in RSpec test - Rails4 Ruby 2.1.2

Was working on an RSpec test that copies #protocol to #dest and saw this:
[26] pry(#<RSpec::ExampleGroups::ProtocolsController::ProtocolCopy>)> #protocol.step_items.count
=> 3
[27] pry(#<RSpec::ExampleGroups::ProtocolsController::ProtocolCopy>)> assigns(:dest).step_items.count
=> 3
[28] pry(#<RSpec::ExampleGroups::ProtocolsController::ProtocolCopy>)> #protocol.step_items[2].note
=> "note3"
[29] pry(#<RSpec::ExampleGroups::ProtocolsController::ProtocolCopy>)> assigns(:dest).step_items[2].note
=> "note3"
[30] displayed an instance variable. Removed for berevity
[31] pry(#<RSpec::ExampleGroups::ProtocolsController::ProtocolCopy>)> #protocol.step_items.last.note
=> "note3"
[32] pry(#<RSpec::ExampleGroups::ProtocolsController::ProtocolCopy>)> assigns(:dest).step_items.last.note
=> "note2"
[33] pry(#<RSpec::ExampleGroups::ProtocolsController::ProtocolCopy>)> assigns(:dest).step_items.last.last?
=> false
[34] pry(#<RSpec::ExampleGroups::ProtocolsController::ProtocolCopy>)>
pastebin of the pry session may be easier to read here.
My gemfile is here.
Seems assigns(:dest).step_items.last is returning the second-to-last item in the array.
Result is reproducible. Looks like a bug to me. Is this some odd RSpec side effect? Is this somehow related to my use of acts_as_list? Either way, it seems like a wrong result for 'last'.

Strange problem with activerecord fetch/find with column name 'changes' in a RAILS 2.3.8 model

How is this possible?
Loading development environment (Rails 2.3.8)
>> wq = Wq.first(:conditions =>['widget_id=? AND qs_id=?',1,1])
=> #<Wq id: 1, widget_id: 1, qs_id: 1, operator: 0, requirements: "2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2", changes: "1", route: 2, created_at: "2010-09-07 08:11:05", updated_at: "2010-11-24 10:25:53", body: "Which specific area of gyt are you aiming to addres...", options: "['xyz','pqr']", input_type: nil, status: 1>
>> wq.changes
=> {}
>> wq.changes
=> {}
>> wq.requirements
=> "2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2"
>> wq.changes
=> "1"
Why is wq.changes coming as null initially and then after logging wq.requirements, wq.changes seems to come fine?
All necessary fields that are being fetched are withing a attr_accessible in the model.
I am not able to understand this situation, please help all you rails gurus.
The attribute name 'changes' conflicts with the AR::Dirty functionality. You should probably pick a different name for that column.
Here's the rails3 api docs for Dirty:
http://api.rubyonrails.org/classes/ActiveModel/Dirty.html
In rails2 it's in ActiveRecord rather than ActiveModel.
If you aren't able to rename the column, you could work around the issue by calling #model_obj[:changes] instead.
https://github.com/rails/rails/blob/master/activerecord/lib/active_record/base.rb#L1466

Resources