moiristo / deep_cloneable

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

Conditional include is ignored #65

Closed Joeventures closed 8 years ago

Joeventures commented 8 years ago

Using this code on an Employer model:

def duplicate(new_name, options = {})
  include_employees = options[:include_employees] || true
  new_employer = self.deep_clone include: [
    employees: [if: lambda{ |x| include_employees }]
  ], validate: false
end

If include_employees is false, employees are still included. Is my syntax incorrect, or is deep_cloneable just ignoring the condition?

moiristo commented 8 years ago

Hi there,

I added a test for this in 02a76657030194f0302f0b639275d01d1920551c, but I couldn't reproduce. Are you sure include_employees is false? When options[:include_employees] is false, include_employees will still be true, as the assignment in line 1 of your code will always yield true. It should be something like:

include_employees = options.fetch(:include_employees, true)