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

Instantiate new records from relation #59

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Aquaj
Copy link

@Aquaj Aquaj commented Jul 22, 2022

Hi !

We encountered an issue recently where we were using NestedHasMany with a polymorphic association that we'd love to fix upstream instead of monkey-patching on our end.

Problem

Since the new record was instantiated from a simple associated_class.new, we had no way of knowing at render time what owner class the record was going to be a child of, and because of that we couldn't customize anything related to that.
Our only option was to build a new model for each of the owner classes, losing the polymorphic association or moving to STI.

Solution

By building from the owner record's relation instead of the associated class, we get a new record that's prefilled with all the data configured into the association (mainly the <x>_type from polymorphic relations and any attributes set by wheres in the association scope).

This allows us to then customize using that data later on (in our case, to specify a dynamic collection for a Field::Select depending on the parent record).

Interestingly, cocoon uses a similar pattern to render the link to add a new form.

@Aquaj
Copy link
Author

Aquaj commented Jul 22, 2022

Hm. I see the CI fails bc it's not able to find the link_to_remove_association but I fail to reproduce the error locally 🤔

Capture d’écran 2022-07-22 à 19 12 19

I'm willing spend some time to try and diagnose this, but just in case I have to ask: I assume it's not a known issue on your CI because of some capybara drivers or something like that ? 😅

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

Successfully merging this pull request may close these issues.

1 participant