# How to create an association through a Drag & Drop

**URL:** <https://www.ruby-forum.com/t/how-to-create-an-association-through-a-drag-drop/163136>\
**Category:** Rails\
**Created:** [March 17, 2009, 4:29pm UTC](https://www.ruby-forum.com/t/how-to-create-an-association-through-a-drag-drop/163136 "2009-03-17T16:29:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sacredceltic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@Sacredceltic](https://www.ruby-forum.com/u/Sacredceltic)\
**Post date:** [March 17, 2009, 4:29pm UTC](https://www.ruby-forum.com/t/how-to-create-an-association-through-a-drag-drop/163136/1 "2009-03-17T16:29:05Z")

</div>

I have 3 tables A, B and C

Table A has\_many Cs  
Table B has\_many Cs  
so C contains 2 foreign keys, A\_id and B\_id)

I have a screen with 2 lists : A list and B list  
I would like to be able to create a C record by simply dropping an A  
element on a B element

I tried this :

 \<% for A in @As %\> \<% draggableA\_id = "draggableA\_#{A.id}" %\> - class="draggableAs"\> \<% A.name %\> 
 \<%= draggable\_element(draggableA\_id, :revert=\>true) %\> \<% end %\>

 \<% for B in @Bs %\> \<% droppableB\_id = "droppableB\_#{B.id}" %\> - class="droppableBs"\> \<%= B.name %\> 
 \<%= drop\_receiving\_element(droppableB\_id, :accept =\> "draggableAs", :with =\> "'draggableA=' + (element.id.split('\_').last())", :url =\> {:action=\>:create\_C} )%\> \<% end %\>

It drops OK, but :

1. The problem is, the “:with” parameter takes only one key. How do I  
convey the second one ? It doesn’t seem to accept a hash…
2. When I debug, I see that the draggableA\_id that I drop is always  
the last draggableA\_id, whichever draggableA I drag…

---

<div class="post-metadata">

**Author:** ![Sacredceltic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@Sacredceltic](https://www.ruby-forum.com/u/Sacredceltic)\
**Post date:** [March 17, 2009, 5:04pm UTC](https://www.ruby-forum.com/t/how-to-create-an-association-through-a-drag-drop/163136/2 "2009-03-17T17:04:24Z")

</div>

On Mar 17, 3:28 pm, Sacredceltic [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> It drops OK, but :
> 
> 1. The problem is, the “:with” parameter takes only one key. How do I  
> convey the second one ? It doesn’t seem to accept a hash…

There’s some examples of the usage of :with here:

> **[:with or :without you: link\_to\_remote's mysterious parameter - Space Vatican](https://www.spacevatican.org/2008/5/17/with-or-without-you-link_to_remote-s-mysterious-parameter/)**
>
> One of the nice things about Rails is that it makes it really easy to get off the ground with Ajax, thanks to the link\_to\_remote helper (and all the …

(it’s mostly about link\_to\_remote but that doesn’t matter)

Fred
