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

  • Home
  • SEARCH
  • 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 117853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:24:07+00:00 2026-05-11T03:24:07+00:00

I have two models: Person and Relation. The second one stores information about relations

  • 0

I have two models: Person and Relation. The second one stores information about relations between two people. It has parent_id and child_id fields and doesn`t have id field. I connected it with has_many :through and it works.

But:

  1. Relation.find(:all) returns empty array even if there are some relations in the table (becouse there is no id field).
  2. I don`t know how to delete relation.

My models looks like this:

class Person < ActiveRecord::Base   has_many :child_relations,             :class_name => 'Relation',             :foreign_key => 'parent_id'   has_many :parent_relations,             :class_name => 'Relation',             :foreign_key => 'child_id'    has_many :children, :through => :child_relations   has_many :parents, :through => :parent_relations end  class Relation < ActiveRecord::Base   belongs_to :parent, :class_name => 'Person'   belongs_to :child, :class_name => 'Person' end 

Any suggestions?

UPDATE: I’ve used has_many :through becouse I also store information about a type of the relation in the table. Currently I gave up and I added id field to my table (Rails convention…). But my question remains open.

  • 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. 2026-05-11T03:24:08+00:00Added an answer on May 11, 2026 at 3:24 am

    has_many :through depends on an id in the join table. It makes the join table a whole model. Since every operation with records happens with an id, you wont be able to directly interface with a table without it. When you delete a record rails generates sql to delete the record by its id. If you have a join table that acts as a full model, it must have an id

    Or you can use has_and_belongs_to_many which works more like you expect. The remove a relationship you remove the object via its assocations and not directly with the relationship model.

    Your best bet is to add an ID to the join table. That way in the future if the relationship model becomes more complex you can track it as its own entity.

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

Sidebar

Related Questions

I have two models: Show and Venue . Show has one venue while each
I have three table in SQL database such as table Person has relation one
Let's say I have two models Event and Person . Many people attend an
I have a Rails simple application that has two main models. A person model
I have two models: Person and Address which I'd like to create in a
I have two models, A and B, and one light, L. I would like
For example I have two model objects, Person and Address. Address has a reference
I have two models in Django linked together by ManyToMany relation like this: class
As an example, I have two models: Household and Person : from django.db import
I have two models in my application, Transaction and Person, with a many-to-many relationship.

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.