moiristo / deep_cloneable

This gem gives every ActiveRecord::Base object the possibility to do a deep clone that includes user specified associations.
MIT License
785 stars 89 forks source link

Rails 5 #76

Closed dsounded closed 7 years ago

dsounded commented 7 years ago

I have troubles with Rails 5 and postgres:

foreign key field is not a number and some other fields are invalid, any ballpark date for Rails 5 compatibility ?

moiristo commented 7 years ago

The test suite is being run on TravisCI using rails 5.0.0.1 and passes. Which version are you exactly talking about?

dsounded commented 7 years ago

5.0.0.1 same, but my locals specs pass on Rails 4.2 and don't pass on 5

I am using 2.2.2 version

moiristo commented 7 years ago

It might have to do with the new rails defaults for belongs_to. But I'm not sure. Do you think you can come up with a failing test case?

dsounded commented 7 years ago

@moiristo From what I see, there is another validation in Rails 5:

I've tried to debug here lib/deep_cloneable.rb:88

kopy.associations.map(&:association_field) (where associations is just a has many relation) [nil, nil, nil, nil] same in Rails 4 and 5

But kopy.associations.map(&:valid?) Rails 4: [true, true, true, true] Rails 5: [false, false, false, false]

That's why it says not a number, 'cause it's nil

dsounded commented 7 years ago

adding config.active_record.belongs_to_required_by_default = false into config didn't solve the problem

moiristo commented 7 years ago

So it tells you that the associated objects are not valid. What are the errors? Can you do something like: kopy.associations.map{|record| record.valid?; record.errors.full_messages.join(',') } and see what the validation errors are?

dsounded commented 7 years ago

@details={:association_id=>[{:error=>:not_a_number, :value=>nil}]}, @messages={:association_id=>["is not a number"]}

moiristo commented 7 years ago

Ok, so kopy.associations points to a certain list of objects that contain a belongs_to somewhere that is stored in association_id. Does it work when you make that belongs_to optional?

dsounded commented 7 years ago

Added optional: true to belongs_to relation, it doesn't work as well, btw I have belongs_to with params class_name and foreign_key if it matters...

moiristo commented 7 years ago

Hmm, it might be that rails5 handles things differently when nil is explicitly being passed to an association. Just a hunch.. I'll try to find out if I can reproduce the error in the test suite.

dsounded commented 7 years ago

Doing the same on new rails 5 project

moiristo commented 7 years ago

👍 please let me know when you know more!

dsounded commented 7 years ago

After deeper digging into the problem I've seen it's my custom validation fails, sorry for that...

thanks for the gem, btw

moiristo commented 7 years ago

Ok, thanks for the feedback!