Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 582393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:40:55+00:00 2026-05-13T14:40:55+00:00

I would like to model a person’s relationship to another person, where the relationship

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T14:40:56+00:00Added an answer on May 13, 2026 at 2:40 pm
    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to be able to model a many-to-many relationship that has extra
Person model has a name field. I would like to get an array of
I would like to keep model binding when doing ajax calls from my view.
I would like to create model binding functionality so a user can enter ','
I would like to design a model for an online private school in Django.
In my model I would like to create a scope that takes advantage of
I would like to make a model agency based on codeigniter, but im a
I would like to destroy a nested model if its attributes are blanked out
I would like to always have my model look to see if an object
I would like to create a Group model in Google App Engine and then

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.