jwood / tenacity

A database client independent way of managing relationships between models backed by different databases.
MIT License
118 stars 17 forks source link

Add support for many_to_many associations #7

Open jwood opened 13 years ago

jwood commented 13 years ago

See ActiveRecord's has_and_belongs_to_many association

abradner commented 12 years ago

This would be particularly useful. Alternatively, is there a clean (t_)has_many :through => :some_crazy_association workaround?

jwood commented 12 years ago

This is on the short list. I've just been busy with other projects the last few months.

There is no has_many :through work around that I'm aware of. Shoot me an email at john@johnpwood.net with some details on what you are trying to do, and perhaps I can make a few suggestions.

abradner commented 12 years ago

I'll post here in case others finds themselves in a similar situation. If it gets too off topic or convoluted then we can just delete them from here and make a wiki page or something.

In an active record Project model I have an AR model for datasets (which are basically just a container that manages permissions and display configuration) and a Mongoid model for raw data, which for this example might be a few columns of data parsed from a .csv file. The raw data can be referenced by many datasets.

The models look a bit like this:

class Project < ActiveRecord::Base
  include Tenacity
  has_many :datasets, :dependent => :destroy
  t_has_many :raw_data_items, :dependent => :destroy
end

class RawDataItem
  include Mongoid::Document
  include Tenacity

  t_belongs_to :project
  t_belongs_to :user
end

class Dataset < ActiveRecord::Base
  belongs_to :project
end

what I would like to do is have this relationship:


class RawDataItem
  include Mongoid::Document
  include Tenacity

  t_belongs_to :project
  t_belongs_to :user
  t_has_many :datasets, :through => :some_join_model
end

class Dataset < ActiveRecord::Base
  belongs_to :project
  t_has_many :raw_data_items, :through => :some_join_model
end

And yes, in an ideal world I would probably want to build everything in mongo, but the product owner is emphatically against the idea, so that's not an option.

jwood commented 12 years ago

Yeah...unfortunately tenacity won't be able to help you here. And, I don't see there being an easy way to address this. You'd have to create that join table, and then write code in the RawDataItem and Dataset classes to fetch the associated objects.

If you were feeling ambitious, you could take a stab at implementing the t_has_many :through behavior in tenacity.