views:

342

answers:

1

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:

  1. How do I define the relationships between these tables correctly?
  2. As the my_id/your_id combination is unique, does it make sense to eliminate the :id on the relationships table?
  3. Are there better names for the 'my_id' & 'your_id' fields to make use of RoR's conventions?
  4. Will I have difficulties with the act_as_tree relationship if one of the columns isn't name 'parent_id'?
+1  A: 
  1. Just a couple of days ago, a similar question was asked: “Many-to-many relationship with the same model in rails?”. I tried to document extensively how to do looped associations there. Perhaps that will help you along?

  2. Tables without IDs in Rails are only ever seen with a has_and_belongs_to_many association. With regular has_many :through associations, the join table's model is like any other ActiveRecord model, and requires an ID column

  3. I'm not aware of a good convention here, but those examples are a bit strange. You'd be accessing them as relationship.your, which feels a bit awkward to me personally. Perhaps your_person_id, which would be accessed as relationship.your_person, and make clear that you're dealing with a Person instance? Another option could be relationship.you.

  4. I've never used acts_as_tree, but you can invoke it with a parameter like so: acts_as_tree :foreign_key => 'my_id'

As I mention in my answer to the other question, it looks like your biggest difficulty will be bi-directional relationships. Once a person A is connected to a person B, it is not implied that person B is connected to person A. That's unfortunately difficult to accomplish in ActiveRecord, from what I can tell.

Shtééf
The simplest way to handle the reciprocal relationship might be to create both directions at the same time. It doubles the size of the database, but at least the lists are available.
Eric Hill