0

I have many-to-many relationship (HABTM) in a file service:

class Album < ActiveRecord::Base
  has_and_belongs_to_many :media, join_table: 'albums_media'
end

class Medium < ActiveRecord::Base
  has_and_belongs_to_many :albums, join_table: 'albums_media'
end

We have a gem containing the ActiveResource interface classes into that service:

class Medium < ActiveResource::Base
  ...

  def albums
    Album.where(id: album_ids)
  end
end

I can easily create a Medium that belongs to a given Album by passing album_ids:

album = Album.create(...)
medium = Medium.create(album_ids: [album.id])

I'd like to be able to delete a certain medium from a given album, but ActiveResource doesn't really support this directly.

# not supported...
medium.albums.destroy(...)

And I'd rather not use has_many :through as I don't really need to manage the join directly other than doing this kind of thing.

Any insight is appreciated.

Midwire
  • 1,090
  • 8
  • 25

1 Answers1

0

FYI: I ended up adding the extra "join" resource in the file service itself, and then adding the corresponding ActiveResource class to the gem. Now we can simply manage the joins directly using the API.

It was more work than I wanted to do but AR is appropriately lite weight and doesn't support many-to-many relationships. The point is, it can be done.

Midwire
  • 1,090
  • 8
  • 25