Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clone always clears inclusion list at apply time, but this isn't documented (& not obviously correct) #75

Open
dmolesUC opened this issue Feb 23, 2017 · 2 comments

Comments

@dmolesUC
Copy link

I want to include certain associations, clone others, and exclude any that aren't explicitly included or cloned. This doesn't seem to be supported, because apply_clones calls exclude_association (via exclude_clone_if_has_many_through), and exclude_association clears the includes array.
This isn't explicitly documented, and I had to discover it by stepping through in the debugger.

It looks from the code as though this is only meant to happen for has_many_through relations. However, even when association.options[:through] is nil, exclude_association(association.options[:through]) still gets called, and exclude_association clears the includes array even if its argument is nil.

It seems like it would be more correct for exclude_clone_if_has_many_through to do something like:

def exclude_clone_if_has_many_through(clone_field)
  association = @object_klass.reflect_on_association(clone_field)

  return unless association.macro == :has_many ||
    association.is_a?(::ActiveRecord::Reflection::ThroughReflection)

  through_assoc = association.options[:through]
  return unless through_assoc

  if amoeba.includes.empty? # we're either in exclusive mode or we want to be
    amoeba.exclude_association(through_assoc)
  else # we're in inclusive mode
    amoeba.includes.delete(through_assoc) # no effect if already not included
  end

but I don't know Amoeba well enough yet to know whether this would have other undesirable side effects.

@dmolesUC
Copy link
Author

(I might also be using clone wrong, but in that case maybe an exception should be raised here? Or when a nil is passed to exclude_association?)

@benjamin-hull
Copy link

benjamin-hull commented Jan 22, 2019

This has bitten me today, too, in a slightly different way:

If there are no calls to include_association or exclude_association, but calls to clone in the config, the model is forced into exclusive mode by the fact that clone clears the list (therefore includes is empty, triggering exclusive mode).

This means you can't only clone associations. As it is, I wonder if calling clone should trigger inclusive mode as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants