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

include_association with array parameter overwrites existing values #76

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

Comments

@dmolesUC
Copy link

dmolesUC commented Feb 23, 2017

Given either of the following:

amoeba do
  include_association :foo
  include_association :bar
  include_association :baz
end
amoeba do
  include_association [:foo, :bar, :baz]
end

@config[:includes] takes the value [:foo, :bar, :baz].

Given this form:

amoeba do
  include_association :foo
  include_association [:bar, :baz]
end

I would expect the same result, but instead the initial [:foo] gets overridden and the final value of@config[:includes] is just [:bar, :baz].

This is counterintuitive, to say the least.

(Not that you'd normally need to do this in the same file, but. We ran into it with an app that monkey-patches additional associations into a rails engine model. Including the app associations was clobbering the inclusion of the original engine associations.)

@dmolesUC
Copy link
Author

A reasonably terse workaround:

amoeba do
  include_association :foo
  [:bar, :baz].each { |a| include_association a }
end

@dmolesUC dmolesUC changed the title push_value_to_array with array parameter overwrites existing values include_association with array parameter overwrites existing values Feb 23, 2017
@dmolesUC
Copy link
Author

I assume this is also an issue for exclude_association and for clone, given they all use push_value_to_array.

@davidguthu
Copy link

This is actually noted in the documentation as intended the way the DSL behaves.

@dmolesUC
Copy link
Author

dmolesUC commented Jun 21, 2017

It's not at all obvious from the documentation that “original values” in

If you pass an array, your array will overwrite the original values

includes any previous calls to include_association, and even if documented, it's not at all obvious that such behavior is desirable.

@davidguthu
Copy link

davidguthu commented Jun 21, 2017

I suppose the author came across a situation where he wanted to override prior settings without having to go through multiple singular calls to exclude_association? Agreed it's not the most intuitive.

@dmolesUC
Copy link
Author

It's a long story, but the model is defined in one engine, then monkey-patched in another to add a bunch more relations, some of which need to be amoeba-duplicated.

@davidguthu
Copy link

Sounds like fun =P Did you end up using this gem despite the issues you had?

@dmolesUC
Copy link
Author

A barrel of laughs. :) Yes, we used the [...].each { |a| include_association a } workaround I mentioned above.

@mattmenefee
Copy link

Pull request #67 added #include_associations and #exclude_associations (note that they're both plural) which seem to intend to be used when multiple associations are to be included or excluded. These new methods aren't documented in the Readme, but using them instead fixed the issue for me.

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

3 participants