clowne-rb/clowne

Which association is created first if you have multiple associations?

huyvohcmc opened this issue · 2 comments

class Member
  has_many :jobs
  has_many :addresses
end
class Job
  ....

  validate :validate_member_address

  def validate_member_address
    VALID_STREET_ADDRESSES.include?(member.current_address.street_name)
  end
end

When cloning a member, the cloner will clone jobs and addresses but the cloned job objects will fail to validate because at the time you clone member.jobs, member.current_address (class Address) has not been cloned yet.

So, is there any way to making sure we will clone member.address first before member.jobs?

class MemberCloner < Clowne::Cloner
  include_association :jobs
  include_association :addresses
end

What does current_address relationship look like? Is this current_address_id in members table? If so, after the cloning addresses relation this value will be broken (member.current_address => nil) and you have to restore it (see: https://clowne.evilmartians.io/#/clone_mapper)

Btw, I think the problem here in the validation itself. The job model should not validate third party data, and probably better to move it to the Address model.

FYI current_address is just the latest address of the member.