# REST and habtm relationship

**URL:** <https://www.ruby-forum.com/t/rest-and-habtm-relationship/82161>\
**Category:** Rails\
**Created:** [January 17, 2007, 9:06pm UTC](https://www.ruby-forum.com/t/rest-and-habtm-relationship/82161 "2007-01-17T21:06:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![enchanter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ce7236/32.png) [@enchanter](https://www.ruby-forum.com/u/enchanter)\
**Post date:** [January 17, 2007, 9:06pm UTC](https://www.ruby-forum.com/t/rest-and-habtm-relationship/82161/1 "2007-01-17T21:06:01Z")

</div>

Hey All

I’m new to the whole RESTful way of thinking so perhaps I’m missing the  
bigger picture here but…

I’ve got a habtm relationship that I’d really like to control RESTfully  
but I’m not sure how best to handle it.

films \<=\> genres

I can’t come up with any logical model for a hmt relationship. How  
would you handle that? Create a films\_genres model perhaps? Is there a  
better way?

Thanks!  
Tim

---

<div class="post-metadata">

**Author:** ![enchanter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ce7236/32.png) [@enchanter](https://www.ruby-forum.com/u/enchanter)\
**Post date:** [January 18, 2007, 12:38am UTC](https://www.ruby-forum.com/t/rest-and-habtm-relationship/82161/2 "2007-01-18T00:38:31Z")

</div>

REST doesn’t really apply to the model level, as far as I know (I’m a  
noob myself with the concept). It’s at the controller level.

Set up your habtm models normally, using the Agile Rails book as an  
example if necessary.

Then implement your RESTful routes which will give you URLs like this:

/films/562/genres/  
/genres/17/films/

The routes would look like this:

map.resources :films do |film|  
film.resources :genres, :prefix\_path =\> ‘film\_’  
end

map.resources :genres do |genre|  
genre.resources :films, :prefix\_path =\> ‘genre\_’  
end

This will allow you to use helpers like film\_genres\_path(@film) to link  
to a list of genres for a specific film. Or, “form\_for :film, :url =\>  
genre\_films\_path(@genre)” to add a new film to a genre. This last  
command may need (@genre,nil) as arguments I’ve found… the RESTful  
stuff seems to sometimes need a blank argument explicitly when there are  
similar routes configured.

---

<div class="post-metadata">

**Author:** ![enchanter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ce7236/32.png) [@enchanter](https://www.ruby-forum.com/u/enchanter)\
**Post date:** [January 18, 2007, 10:07am UTC](https://www.ruby-forum.com/t/rest-and-habtm-relationship/82161/3 "2007-01-18T10:07:52Z")

</div>

> I’ve got a habtm relationship that I’d really like to control  
> RESTfully  
> but I’m not sure how best to handle it.

REST hasn’t changed the way habtm relationships work. You can implement  
the habtm models as before. Then create the routes like so:

map.resources :genres do |genre|  
genre.resources :films  
end

If you scaffold the resources, they’ll need a little tweaking to get the  
paths right but there’s plenty of information on how to proceed out  
there. (There’s a fine example in AWDWR 2nd.)

Roderick

–  
Nedforce Informatica Specialisten B.V.

> **[De domeinnaam Nedforce.nl is te koop.](http://www.nedforce.nl)**
>
> Deze mooie domeinnaam kan al over een paar minuten van jou zijn. Betalen gaat eenvoudig en vertrouwd via iDEAL. Direct na de betaling ontvang je de verhuiscode per e-mail en kun je aan de slag.

+31 (0)53 4500225

---

<div class="post-metadata">

**Author:** ![enchanter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ce7236/32.png) [@enchanter](https://www.ruby-forum.com/u/enchanter)\
**Post date:** [January 18, 2007, 11:57am UTC](https://www.ruby-forum.com/t/rest-and-habtm-relationship/82161/4 "2007-01-18T11:57:23Z")

</div>

Tim McIntyre wrote:

> I’ve got a habtm relationship that I’d really like to control RESTfully  
> but I’m not sure how best to handle it.
> 
> films \<=\> genres
> 
> I can’t come up with any logical model for a hmt relationship. How  
> would you handle that? Create a films\_genres model perhaps? Is there a  
> better way?

> From my understanding, the RESTful way of doing this would be to create  
> a join model, perhaps called Categorisation, and then use that instead  
> of a HABTM. If you created a Categorisation model that belongs to both  
> film and genre, with both of those having has\_many xxx, :through =\>  
> :categorisation. You can then create a Categorisations Controller with  
> the relevant CRUD methods to handle the film \<=\> genre relationship.  
> For example, creating a new Categorisation effectively relates to  
> assigning a particular genre to a film.

Ben
