# Many-to-many relationship the REST way

**URL:** <https://www.ruby-forum.com/t/many-to-many-relationship-the-rest-way/117225>\
**Category:** Rails\
**Created:** [October 15, 2007, 12:58am UTC](https://www.ruby-forum.com/t/many-to-many-relationship-the-rest-way/117225 "2007-10-15T00:58:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![davosian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f05b48/32.png) [@davosian](https://www.ruby-forum.com/u/davosian)\
**Post date:** [October 15, 2007, 12:58am UTC](https://www.ruby-forum.com/t/many-to-many-relationship-the-rest-way/117225/1 "2007-10-15T00:58:04Z")

</div>

I have two models, Player and Event. A player can subscribe to several  
events and a event has several participants (players). Therefore, it  
is a many-to-many relationship. My question now is how do I have to  
implement this many-to-many relationship using the REST approach.

I do not need to store additional information in the bridge table  
(let’s call it “subscriptions” or “events\_players”), therefore I added  
“has\_and\_belongs\_to\_many” to the player and the events model. What I  
am now missing is a “subscribe” action, that I would normally stick  
into the player controller, but this violates the REST pattern,  
because it is not a CRUD action.

The only way around this, that I see, is to make subscription a full  
blown model with its own RESTful controller. This would give me CRUD  
operations on “subscriptions”, with new/create being the equivalent to  
a subscribe action, correct?

Is it therefore correct to say, that “has\_and\_belongs\_to\_many” cannot  
be used in RESTful applications?

What are your thoughts on this?

Thanks,  
D.

---

<div class="post-metadata">

**Author:** ![davosian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f05b48/32.png) [@davosian](https://www.ruby-forum.com/u/davosian)\
**Post date:** [October 15, 2007, 2:34am UTC](https://www.ruby-forum.com/t/many-to-many-relationship-the-rest-way/117225/2 "2007-10-15T02:34:30Z")

</div>

i think you’re being too stiff 😉

yes, you could make a ‘subscriptions’ controller if you want to, or  
just throw some subscribe/unsubscribe methods into the other  
controllers.

point being, it’s your app, you (and maybe others) have to maintain  
it, so do what you think makes sense. there’s no RESTful police

---

<div class="post-metadata">

**Author:** ![davosian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f05b48/32.png) [@davosian](https://www.ruby-forum.com/u/davosian)\
**Post date:** [October 15, 2007, 2:57am UTC](https://www.ruby-forum.com/t/many-to-many-relationship-the-rest-way/117225/3 "2007-10-15T02:57:52Z")

</div>

To retain a fully RESTful interface, you need a subscriptions  
controller. If there isn’t any other “extra” information on the  
subscriptions controller, you could simply create a subscribe method  
or whatever on one of the models, but typically in REST design, you’d  
create an independent controller.

–Jeremy

On 10/14/07, jemminger [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > into the player controller, but this violates the REST pattern,  
> > What are your thoughts on this?
> > 
> > Thanks,  
> > D.
> 
> >

–  
[http://www.jeremymcanally.com/](http://www.jeremymcanally.com/)

My books:  
Ruby in Practice

> **[Ruby in Practice](https://www.manning.com/books/ruby-in-practice)**
>
> Like Ruby itself, Ruby in Practice will make you more productive. The book shows you practical techniques and strategies for small projects and large-scale environments. A cookbook-style reference, it gives you concrete examples of systems...

My free Ruby e-book

> **[Site not found · DreamHost](http://www.humblelittlerubybook.com/)**
>
> The owner of this domain has not yet uploaded their website.

My blogs:

> **[Site not found · DreamHost](http://www.mrneighborly.com/)**
>
> The owner of this domain has not yet uploaded their website.

[http://www.rubyinpractice.com/](http://www.rubyinpractice.com/)

---

<div class="post-metadata">

**Author:** ![davosian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f05b48/32.png) [@davosian](https://www.ruby-forum.com/u/davosian)\
**Post date:** [October 15, 2007, 3:23am UTC](https://www.ruby-forum.com/t/many-to-many-relationship-the-rest-way/117225/4 "2007-10-15T03:23:00Z")

</div>

On 10/14/07, davosian [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> >

Controllers don’t necessarily have a 1:1 mapping to models. For  
example, you could quite easily have

class RegistrationsController \< ActionController::Base  
def create  
e = Event.find(params[:id])  
current\_user.events \<\< e  
flash[:success] = “You’ve registered for #{e.name}”  
redirect\_to user\_home\_url  
end  
end

You’ve got an easy to use API, and your model code can stay how it is.  
If your model changes to store extra attributes on the registration,  
the API stays the same (except for passing in extra, perhaps optional,  
attributes). It’s also flexible and obvious how to extend it if/when  
you want to treat registrations as a resource of their own.

Pat

---

<div class="post-metadata">

**Author:** ![davosian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f05b48/32.png) [@davosian](https://www.ruby-forum.com/u/davosian)\
**Post date:** [October 15, 2007, 1:45pm UTC](https://www.ruby-forum.com/t/many-to-many-relationship-the-rest-way/117225/5 "2007-10-15T13:45:56Z")

</div>

I like that idea, thank you!

---

<div class="post-metadata">

**Author:** ![davosian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f05b48/32.png) [@davosian](https://www.ruby-forum.com/u/davosian)\
**Post date:** [October 15, 2007, 2:59am UTC](https://www.ruby-forum.com/t/many-to-many-relationship-the-rest-way/117225/6 "2007-10-15T02:59:37Z")

</div>

Nice way of putting it 😉

Isn’t one of the advantages of using REST that this automatically  
creates an easy to use API? If I introduce “non-standard” commands,  
wouldn’t that break the conventions?

Where do you see the advantages / disadvantages for adding to an  
existing controller versus creating a “many-to-many” controller?
