Created
February 26, 2012 00:54
-
-
Save melriffe/1911966 to your computer and use it in GitHub Desktop.
Notes on changing a Many-to-Many to a Self-Referential Many-to-Many
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Currently I have Kits and Items. A Kit can contain many Items; an Item can be associated to many Kits. | |
I also have a join model, KitItem, that allows me to define how many of an Item is associated to a Kit. | |
Here are the model definitions: | |
class Kit < ActiveRecord::Base | |
belongs_to :category | |
has_many :kit_items, :dependent => :destroy | |
has_many :items, :through => :kit_items | |
end | |
class Item < ActiveRecord::Base | |
belongs_to :category | |
has_many :kit_items, :dependent => :destroy | |
has_many :kits, :through => :kit_items | |
end | |
class KitItem < ActiveRecord::Base | |
belongs_to :item | |
belongs_to :kit | |
end | |
# == Schema Information | |
# | |
# Table name: kit_items | |
# | |
# id :integer(4) not null, primary key | |
# quantity :integer(4) | |
# kit_id :integer(4) | |
# item_id :integer(4) | |
# created_at :datetime | |
# updated_at :datetime | |
# | |
# Indexes | |
# | |
# index_kit_items_on_item_id (item_id) | |
# index_kit_items_on_kit_id (kit_id) | |
# | |
However, I need to support the concept of a Kit containing other Kits. | |
I'm not sure how to implement this. The examples I've seen so far are for single table self-joins; | |
I've only confused myself when attempting to alter them for my purposes. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm definitely studying this less for the code and more for the magical incantation to summon @bokmann