Forum: Ruby on Rails REST - Implementing HABTM in the real world

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
73d4d83b7e74797ad6699c39ad5c5ffe?d=identicon&s=25 Tom Taylor (Guest)
on 2007-03-12 11:28
(Received via mailing list)
Hi again - another REST question!

I think I've got a good grasp on it all now, except for how to
implement HABTM properly. I understand you need turn the join model
into a controller, such as 'memberships' below, and then use CRUD on
that to perform joining actions.

class Group < ActiveRecord::Base

  has_many :memberships
  has_many :users, :through => :memberships

class User < ActiveRecord::Base

  has_many :memberships
  has_many :groups, :through => :memberships

class Membership < ActiveRecord::Base

  belongs_to :user
  belongs_to :group

What I don't understand is how that actually works in your code. Let's
say I'm looking at all of the groups (GET /groups), and want to get
the current user to join one. Presumably, I'd POST to /memberships?

But how would I handle if that failed, or they didn't have permission
to do so? And how would I handle the controller returning to the page
it came from? Because I could hit the same action from both sides of
the HABTM relationship. (User joining a group, and group selecting a

Are there any examples of this out there?

Thanks a lot!

This topic is locked and can not be replied to.