I would like to model a person's relationship to another person, where the relationship isn't necessarily hierarchical (i.e. friends & colleagues, rather than parent & children) and I am interested in capturing more detail about each relationship (e.g. notes, type of relationship, date established). Finally, I would like to use the act_as_tree relationship to be able to navigate/diagram these relationships.
The migrations:
class CreateProfiles < ActiveRecord::Migration def self.up
create_table :profiles do |table|
table.column :firstName, :string
table.column :lastName, :string
table.column :telephone, :string
table.column :emailAddress, :string
table.column :location, :string
table.timestamps
end end def self.down
drop_table :profiles end end
class Relationship < ActiveRecord::Migration def self.up
create_table :relationships, :id => false do |table|
table.column my_id :integer
table.column your_id :integer
table.column relationshipType :string
table.column dateEstablished :date
table.column notes :text
table.timestamps end end def self.down
drop_table :relationships end end
The models:
class Person < ActiveRecord::Base
has_many :relationships, :foreign_key => "my_id"
has_many :persons :through => :relationships
end
class Relationship < ActiveRecord::Base
belongs_to :persons
acts_as_tree
end
Questions:
- How do I define the relationships between these tables correctly?
- As the my_id/your_id combination is unique, does it make sense to eliminate the :id on the relationships table?
- Are there better names for the 'my_id' & 'your_id' fields to make use of RoR's conventions?
- Will I have difficulties with the act_as_tree relationship if one of the columns isn't name 'parent_id'?