Reassign an existing child from the parent form

Hello, I have been trying to find a solution to this for some time. What
I am trying to do is to have a parent form that can “reassign” child
objects previously assigned to other parent objects.

Here’s a use case: say you had an app for a grocery list. aisles are the
parent and items are the child. An aisle has_many items, an item
belongs_to an aisle.

You had previously assigned cilantro as being in aisle 5, but then you
discover it’s in aisle 3, not aisle 5.

You are on the edit page for aisle 3 and want to be able to reassign
cilantro to it in one action from there, rather than leaving the aisle
three form, and navigating to the cilantro form and changing the parent.

I am curious as to whether anyone has done something like this before,
and what the suggestions would be on how to start.

Is this even possible, or is it an inherent limitation of rails that the
assignment of a child to a parent can only take place by either creating
the child in on a nested parent form, or selecting the parent on the
child form?

Jesse B. wrote in post #1009493:

Hello, I have been trying to find a solution to this for some time. What
I am trying to do is to have a parent form that can “reassign” child
objects previously assigned to other parent objects.

Here’s a use case: say you had an app for a grocery list. aisles are the
parent and items are the child. An aisle has_many items, an item
belongs_to an aisle.

You had previously assigned cilantro as being in aisle 5, but then you
discover it’s in aisle 3, not aisle 5.

You are on the edit page for aisle 3 and want to be able to reassign
cilantro to it in one action from there, rather than leaving the aisle
three form, and navigating to the cilantro form and changing the parent.

It seems inefficient to make the user do all this navigation in the
first place. Yes, I get that having an edit page per aisle would be what
you got from a scaffolding style application, but from a user
perspective that’s nuts.

Just from my quick thought experiment I would build a UI that listed all
aisles on a single page. Each
aisle would be represented by a column on the page listing the items
contained within that aisle. The user would then organize their items in
the store by dragging them from one aisle to another. Each aisle column
might have an add button that performs a search for adding new items to
that aisle.

±-----------±-----------±-----------+
| Aisle 1 | Aisle 2 | Aisle 3 |
±-----------±-----------±-----------+
| Cilantro | Eggs | |
| | Milk | |
| | | |
±-----------±-----------±-----------+
| [Add] | [Add] | [Add] |
±-----------±-----------±-----------+

=> Drag Cilantro from Aisle 1 to Aisle 3

±-----------±-----------±-----------+
| Aisle 1 | Aisle 2 | Aisle 3 |
±-----------±-----------±-----------+
| | Eggs | Cilantro |
| | Milk | |
| | | |
±-----------±-----------±-----------+
| [Add] | [Add] | [Add] |
±-----------±-----------±-----------+

At this point you might ask, “Isn’t this a lot more work on the part of
the developer?” Well, sure it is. But, as a user this is the sort of
functionality that I would expect when presented with the task of
organizing items into aisles. There are a number of client-side tools
that would make building something like this, not only doable, but not
nearly as difficult as it might seem at first glance. There are
drag-and-drop objects in jQuery that would help with this sort of
interface.

Thank you Robert.
I agree that that would be a great interface for an application like
what I described.

What I am looking for is some insight into the rails coding of this,
even as something as simple as having a select menu or checkboxes on the
aisle edit page which lists items and allows you to assign them to the
aisle.

The issue I run into is that the foreign key (aisle_id) which joins the
two models would be on the child(item) model. That is why it’s much
easier to code it where the selection takes place on the item form.

So what I want is a way to have a list of all items on the aisle edit
form, and a way to assign the “id” of the current aisle as the
“aisle_id” of the selected item.