Topic: Triple Join

How would I represent the :has_many_and_is_a 'products_lookup_options' in this triple table join?

Basic explanation of my situation:
Products/lookup relationship: Have multiple types of products (recorded in lookup where type=product_type).

Lookup/options relationship: Each type has a set of properties (which may or may not be set -- can be null). So, options declares what lookup.type values are associated with the specific 'product_type', and in what order (you can have multiple uses for the same dataset in some cases eg. transmit and receive frequency).

Triple join: The triple join table links the lookup_id to the option_id where lookup.type = options.lookup_type, and those are linked to the product where product.lookup_id = options.lookup_id.

Hope that makes some level of sense.

Last edited by KittyKate (2007-03-06 17:34:35)

Re: Triple Join

You'll need to make the triple join a full model.

class Product
  belongs_to :owner
  belongs_to :lookup
  has_many :product_lookup_options
  has_many :options, :through => :product_lookup_options
end

class Lookup
  has_many :products
  has_many :options
  has_many :product_lookup_options
end

class Option
  belongs_to :lookup
end

class ProductLookupOption
  belongs_to :lookup
  belongs_to :product
  belongs_to :option
end


That's a direct model based on the diagram. You can add has_many :through associations to help out a little more. I don't think you will get any further built-in support from Rails with this schema, but there may be a plugin to help out - I don't know.

There might also be a better way to do this. I would offer some suggestions but I'm not exactly sure how these models are being used. If you give a couple examples of what you are trying to accomplish I may be able to help further.

Railscasts - Free Ruby on Rails Screencasts

Re: Triple Join

Would it be appropriate to further refine the properties of Product to:

class Product
  belongs_to :lookup, :conditions => "type = 'product_type'"
  has_many :lookup, :through => :product_lookup_options
  ...
end

Re: Triple Join

I suppose you could, but I don't fully understand how the lookup and option tables work.

Railscasts - Free Ruby on Rails Screencasts