Forum: Ruby on Rails REST and habtm relationship

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Tim M. (Guest)
on 2007-01-17 22:06
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
Carl J. (Guest)
on 2007-01-18 01:38
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.
Roderick van Domburg (Guest)
on 2007-01-18 11:07
(Received via mailing list)
> 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.
http://www.nedforce.nl
+31 (0)53 4500225
Ben (Guest)
on 2007-01-18 12:57
(Received via mailing list)
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
This topic is locked and can not be replied to.