# Update HABTM associations the restful way

**URL:** <https://www.ruby-forum.com/t/update-habtm-associations-the-restful-way/193854>\
**Category:** Rails\
**Created:** [August 25, 2010, 3:44pm UTC](https://www.ruby-forum.com/t/update-habtm-associations-the-restful-way/193854 "2010-08-25T15:44:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dieguinsss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e0b2c6/32.png) [@dieguinsss](https://www.ruby-forum.com/u/dieguinsss)\
**Post date:** [August 25, 2010, 3:44pm UTC](https://www.ruby-forum.com/t/update-habtm-associations-the-restful-way/193854/1 "2010-08-25T15:44:26Z")

</div>

Hello,

## Sorry if this has been answered in old posts, I couldn’t find anything. This has been killing me all the morning ☹ I’m a bit new to REST, and I’m trying to model my app routes ‘restfully’, but I get lost on many things. I’ve been searching how to do this with resources: I have a Service model that has many Sections, and Sections have and belong to many Contents. But the Contents don’t have to be in a Section (it’s optional). I want to be able to add a content to a section, and remove a content from a section. With nested resources I could do something like this:

## map.resources :services, :shallow=\>“true” do |service| service.resources :sections do |section| section.resources :contents end end map.resources :contents

## But it isn’t what I’m searching. I want to do something that allows me to have a route like “POST /services/32/sections/14/add\_content/5”, because “POST /services/32/sections/14/contents/” would create a content in the section 14, but I don’t want a new one. I only want to add an existing content to a existing section (or delete it). I saw somewhere a forum post proposing something like (adapted to this case):

## map.resources :services, :shallow=\>“true” do |service| service.resources :sections, :member=\>{:add\_content=\>:post, :remove\_content=\>:delete} do |section| end end map.resources :contents

But the “member” functions only allow me to operate on the section, and  
I cant specify the content id (only by giving a ?content\_id=5 after the  
URL). Is there anything I can do without falling back to map.connect?

Best regards and thanks in advance,

Diego.

---

<div class="post-metadata">

**Author:** ![dieguinsss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e0b2c6/32.png) [@dieguinsss](https://www.ruby-forum.com/u/dieguinsss)\
**Post date:** [August 26, 2010, 4:28am UTC](https://www.ruby-forum.com/t/update-habtm-associations-the-restful-way/193854/2 "2010-08-26T04:28:29Z")

</div>

If you are adding member methods like “add\_content” and  
“delete\_content” then you have left REST behind.

I understand that a Service is a resource that has many Sections. The  
content already exists, but you want to add it to another section.

HABTM associations are usually processed by a list of checkboxes named  
“section[contents][]”. So in your edit form for the Section, you offer  
the checkboxes. In the update action of the Section, the checkboxes  
get processed automatically when you update attributes.

---

<div class="post-metadata">

**Author:** ![dieguinsss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e0b2c6/32.png) [@dieguinsss](https://www.ruby-forum.com/u/dieguinsss)\
**Post date:** [August 26, 2010, 8:34am UTC](https://www.ruby-forum.com/t/update-habtm-associations-the-restful-way/193854/3 "2010-08-26T08:34:41Z")

</div>

David Furber wrote:

> If you are adding member methods like “add\_content” and  
> “delete\_content” then you have left REST behind.
> 
> I understand that a Service is a resource that has many Sections. The  
> content already exists, but you want to add it to another section.
> 
> HABTM associations are usually processed by a list of checkboxes named  
> “section[contents][]”. So in your edit form for the Section, you offer  
> the checkboxes. In the update action of the Section, the checkboxes  
> get processed automatically when you update attributes.

I understand that the add/remove thing is a bit against REST philosophy,  
but I hoped that some intermediate solution could exist.  
I read about the checkboxes, but a section could have as much as  
thousands of contents. In this case, obviously I can’t offer the  
checkboxes. Even if I leave some of them hidden, I don’t know if sending  
a request with thousands of content\_ids is a good thing :S

If there isn’t a compromise solution in REST I guess I’ll have to stick  
to a more ‘classical’ approach on this. ☹

Any other advice?

Thank you so much.

---

<div class="post-metadata">

**Author:** ![dieguinsss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e0b2c6/32.png) [@dieguinsss](https://www.ruby-forum.com/u/dieguinsss)\
**Post date:** [August 26, 2010, 11:58am UTC](https://www.ruby-forum.com/t/update-habtm-associations-the-restful-way/193854/4 "2010-08-26T11:58:04Z")

</div>

You could try to integrate with a JavaScript solution like  
[http://j.mp/4LdS8r](http://j.mp/4LdS8r)  
(see the example) then you should send the data to the server like in  
the checkboxes way: “sections[contents][]”.  
Here’s the link of a simple jQuery plugin I created for that (it’s a  
little incomplete) [GitHub - samflores/tagger.js: Tagger.js is a jQuery plugin for creating/using tags in forms](http://github.com/samflores/tagger.js/)

---

<div class="post-metadata">

**Author:** ![dieguinsss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e0b2c6/32.png) [@dieguinsss](https://www.ruby-forum.com/u/dieguinsss)\
**Post date:** [August 26, 2010, 12:50pm UTC](https://www.ruby-forum.com/t/update-habtm-associations-the-restful-way/193854/5 "2010-08-26T12:50:03Z")

</div>

Samuel Soares flores wrote:

> You could try to integrate with a JavaScript solution like  
> [http://j.mp/4LdS8r](http://j.mp/4LdS8r)  
> (see the example) then you should send the data to the server like in  
> the checkboxes way: “sections[contents][]”.  
> Here’s the link of a simple jQuery plugin I created for that (it’s a  
> little incomplete) [GitHub - samflores/tagger.js: Tagger.js is a jQuery plugin for creating/using tags in forms](http://github.com/samflores/tagger.js/)

Thank you Samuel -your plugin seems ok to me-, but the problem remains  
the same: I have sections with over 4,000 contents, and I think it’s too  
much load to use it in the normal requests. I’m thinking in modelling  
the habtm relationship between section and content with an independent  
object, and change the habtm to has\_many :through. That way, I believe I  
can keep the REST pholosophy, but it feels someway ‘unnatural’ on the  
model.  
I’ll stay tuned to the advices of the rest of the rest gurus  
(ba-dum-tsh!) 🙂

Regards
