6

we upgraded our Application from Rails 4.1.14 to 4.2.5.1 and hit the following issue:

string = "SomeString"
ar_model = SomeArModel.new
ar_model.some_attribute = string

# next line is true for 4.1, but fails for 4.2
ar_model.some_attribute.object_id == string.object_id

Apparently, object setters dup every object (if I have an array, every object inside will be duped as well) and I wonder, if this is intended and part of some new security feature?

Update

I use ruby-2.2.2p95 for both rails version. For reference I did a small project:

rails new testproject
rails g model Building name:string
rails db:migrate
rails c
  >> b = Building.new
  >> name = "Testname"
  >> b.name = name
  >> name.object_id # => 70199493308960
  >> b.name.object_id # => 70199493278780

Afterwards, I only changed Rails version to 4.1.14 in Gemfile, and tried again => both object_ids were the same. So it can't rely only on the Ruby version...

Update2

It also holds true for ruby-2.2.3 and JRuby 9.0.4.0... ar_model.attributes_before_type_cast['some_attribute'] contains the real object.

M. Koerner
  • 73
  • 4
  • What is your exact Rails version? As I Checked for Rails 4.2.5 the above expression returns True. Secondly I don't think this has anything to do with Rails. It is Ruby job to handle object reference/Memory management – Qaisar Nadeem Feb 02 '16 at 13:14
  • 1
    I double checked with 4.1.14 and 4.2.0 and 4.2.5.1; the latter two return false. I only upgraded rails and devise, no other gems or ruby – M. Koerner Feb 02 '16 at 13:17
  • This is more like a Ruby issue. Remember that, ruby is always pass-by-value instead of pass-by-reference. For further reading check this out: http://stackoverflow.com/questions/1872110/is-ruby-pass-by-reference-or-by-value – aliibrahim Feb 02 '16 at 13:23
  • 3
    Did you mean to use `b.name.object_id`, not `b.object_id`? – Sergio Tulentsev Feb 02 '16 at 13:36
  • Can you check value `b.changes` in both earlier rails version and new version? Not sure whether it is related to - https://github.com/rails/rails/issues/12505 – Wand Maker Feb 02 '16 at 13:39
  • `b.changes` is empty after object creation. As soon as I change the object, `b.name` and `b.changes['name'][1]` are the same (in both 4.1 and 4.2) – M. Koerner Feb 02 '16 at 14:00
  • Can you compare `object_id` of `string` and `ar_model.some_attribute_before_typecast`? – BroiSatse Feb 02 '16 at 14:01
  • @SergioTulentsev Thanks, of course – M. Koerner Feb 02 '16 at 14:21

1 Answers1

0

According to Rails developers, this is by design:

https://github.com/rails/rails/issues/23430

M. Koerner
  • 73
  • 4