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

Headache with "has_many through" associations #616

Open
MissHenesy opened this issue Dec 30, 2021 · 5 comments
Open

Headache with "has_many through" associations #616

MissHenesy opened this issue Dec 30, 2021 · 5 comments

Comments

@MissHenesy
Copy link

Hey, Nathan!

I'm attempting to use Cocoon's dynamic goodness with a very simple demo project that consists of 3 models.
The only problem I'm running into is that my tables have a "Document | Sections | Paragraphs" structure.

Because Documents have many Sections, and Sections have many Paragraphs, I'm only able to render Documents --> Sections with "link_to_add_association"; when I try to render Documents --> Paragraphs, I receive an error::

"Cannot modify association 'Document#section_paragraphs' because the source reflection class 'Paragraphs' is associated to 'Section' via :has_many.

I can't really change my associations, because it is just true that 1 Document will have many Sections, and 1 Section will have many Paragraphs. Sections don't belong to Paragraphs, but since it's being used as a "through", Rails is getting (and giving) a headache.

Is there a way around this? I'm tempted to just put a *#@ document_id in the Paragraphs table, but I'd like to avoid that, if possible.

Thank you!

@nathanvda
Copy link
Owner

Can a paragraph also be linked to a document directly? If i understand correctly, it cannot. You can easily, without problems, have a nested form mulitple levels deep. So when editing the document, show the sections, and the link_to_add a new section. In the partial for the section, you show the nested paragraphs, and a link to add a new paragraph.

If that does not work, please show for instance your view-code, so we can continue from that.

@MissHenesy
Copy link
Author

Thank you so much for responding (and so quickly, too).

So, rather than nest Paragraphs as a subform of Sections, I'd prefer to lay these 2 forms out side-by-side.
Like this::

          Document Form

Section Form Paragraph Form


And this is what is causing my pain.
I'm trying to go right from Documents --> Paragraphs, which is causing Rails (and me) to cry.

In my case, I'm using::

  • Graph (like the Document)
  • Graph Shapes (like Sections)
  • Graph Shape Connectors (like Paragraphs... except these have 2 foreign_key fields to Graph Shapes, since they need to connect_from and connect_to each shape)

Here's how that looks in the models::

Screen Shot 2021-12-30 at 2 31 32 AM

Screen Shot 2021-12-30 at 2 32 50 AM

Screen Shot 2021-12-30 at 2 35 29 AM

And in the Views, Cocoon is working beautifully on the Graph Shapes.... I've just given up on the Graph Connector side.

There's too much to try to screenshot & paste in here, so I'm attaching the views in a Zip file.

I have a strong suspicion that what I'm trying to do isn't really possible, but it would save me a lot of lost sleep just getting a definitive answer on that ;-)

Thank you so much!

graph_views.zip

@nathanvda
Copy link
Owner

nathanvda commented Dec 31, 2021

Ok, please explain then, if your database is structured as

  • document has_many sections
  • section has_many paragraphs
  • and of course, then a document has_many paragraphs through sections

This way you can easily edit the paragraphs and sections side by side, but when you are creating a new paragraph, should that not have the context of a section? Also: what is a meaning of a paragraph without their section.

And of course: since your objects are different, but I imagine it is the same right: you can easily list all the graphshapes and the connectors, but when you try to create a new connector, it has ultimately need the context of a graphshape to be created, no? So imho the side by side option falls flat at that moment.

Mind you, you could do something using xhr/remote forms, let the user choose from a list of graph-shapes to connect from and to, but cocoon will not be much help in that scenario.

@MissHenesy
Copy link
Author

You have read my mind, as I am now fighting with Rails 6 to display the Graph Connector "new" form in a modal window. I think this is the best way, since a side-by-side scenario makes the most sense to me.

See, the relationships of my objects and Document-Sections-Paragraphs are almost exact, except that, for my scenario::
A "Graph Connector" (which is really a "Shape Connector") must start from 1 Graph Shape, and end at 1 other Graph Shape.

So, imagine, you have 3 or 4 Shapes, and you want to start connecting them. Wellllll, your new line (the Shape Connector) can start at Shape 1 and end at Shape 2, or it can end at Shape 3, or you can change it to start at Shape 3 and go to Shape 2.... see ... it's not quite as rigid as Paragraphs are to Sections. The Connectors are very fickle and change their minds about which Shapes they belong to :-)

That's why it's too hard for me to envision putting the Connectors nested under Shapes, because they're meant to be super-changeable.

I'm glad to know we agree that some kind of modal window for the Connectors is my best way for me to have my Connectors next to, rather than nested under, the Shapes.

Now if I could just get the modals to work! Oh, Rails 6....augh! Lol :-)

Thank you so much for your help! I am so glad I had a chance to sound all of this out with you. Cocoon rocks!

@nathanvda
Copy link
Owner

But still one could argue that the "connector", implementing a connection, is maybe less relevant (in the view) as the connection itself.

So one could go for a form like this:

  • FORM
    • shape 1
      • receiving from
        • shape 3
        • add new shape sending here
      • directing to
        • shape 4
        • add new shape to send to

Enfin: struggling a bit with the naming, but such a form would be possible, since you have the correct has-many relationships.

Of course ideally you would show and edit this graphically.

Also not entirely sure still if cocoon is the ideal candidate for this, since it edits a form in its entirety and as such it is not very well in showing a "dynamic" state. E.g. if you add a connection, it will change the state of the shapes immediately? And maybe also the possible shapes to connect will alter when a connection is added or removed.

Thank you for this different problem to think about, that is always inspiring for me too.

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