Model a person-to-person relationship in Ruby-on-Rails using has_many :through
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 开发者_StackOverflow社区=> :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'?
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?
Tables without IDs in Rails are only ever seen with a
has_and_belongs_to_many
association. With regularhas_many :through
associations, the join table's model is like any other ActiveRecord model, and requires an ID columnI'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. Perhapsyour_person_id
, which would be accessed asrelationship.your_person
, and make clear that you're dealing with aPerson
instance? Another option could berelationship.you
.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.
精彩评论