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 3302408
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:50:55+00:00 2026-05-17T20:50:55+00:00

I have a parent/child relationship via our users table, with models as such: class

  • 0

I have a parent/child relationship via our users table, with models as such:

class User < ActiveRecord::Base

  # Parents relationship
  has_many :children_parents, :class_name => "ParentsChild", :foreign_key => "child_id", :dependent => :destroy
  has_many :parents, :through => :children_parents

  # Children relatiopnship
  has_many :parents_children, :class_name => "ParentsChild", :foreign_key => "parent_id", :dependent => :destroy
  has_many :children, :through => :parents_children
  ...
end

And in parents_child.rb:

class ParentsChild < ActiveRecord::Base

  belongs_to :parent, :class_name => "User"
  belongs_to :child, :class_name => "User"

end

Right now, it is possible in our “add children” form (just using vanilla nested attributes) to add the same user as a child multiple times for parents. I am not sure what the ‘right’ way to go about forcing uniqueness in the ParentsChild relationship, although I am leaning towards a unique index on (parent_id, child_id) at the database layer (using a migration of course).

I am sure I could also enforce uniqueness constraints in the UsersController::update method, but would prefer to avoid changing that code (right now it doesn’t reference parents/children at all, thanks to nested attributes in the form/model) if possible. I am most concerned with making sure we use the “proper” solution. What is the ‘right’ or ‘rails’ way to do this?

  • 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-17T20:50:55+00:00Added an answer on May 17, 2026 at 8:50 pm

    Using has_many :through, you can specify :uniq as an option, like this:

      has_many :parents, :through => :children_parents, :uniq => true
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a parent-child relationship: @Entity @Table(name = user) public final class User {
I have parent/child relationship set up via Node Reference. A Child record can have
I have a Parent/Child Table relationship with the ChildTable linking back to the ParentTable
I have a parent child table relationship. In the example below Foo has a
I have a parent child relationship between 2 classes Parent @Entity @Table(name = PARENT)
Suppose I have objects that have a parent-child relationship like this: public class Node
I have a parent/child relationship defined which, unfortunately, cannot be maintained via a foreign
I have a parent-child relationship in an Oracle 9i database-table like: parent | child
I have a parent / child relationship in my application class Polling has_many :alerts,
I have a parent-child relationship, such as Order to Order Item, which I have

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.