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 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

Ask A Question

Stats

  • Questions 75k
  • Answers 75k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer The order-by precedence is left to right so, if you… May 11, 2026 at 2:35 pm
  • added an answer You need a function that does something like return mystring.replace(/&/g,… May 11, 2026 at 2:35 pm
  • added an answer Sounds like the old dev team is biased. I would… May 11, 2026 at 2:35 pm

Related Questions

I have a relationship in a Core Data model that feels like it wants
This question is related to (but perhaps not quite the same as): Does Django
I have a somewhat ridiculous question regarding DDD, Repository Patterns and ORM. In this
OK. I just tied myself in a knot trying to make a form in

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.