1

I'm trying to determine the appropriate relationship to use in Rails. I'm going to have Users who can belong to one other User (their spouse). They will both be of the same model type (User).

I want to be able to call User.spouse on either user and get the user that is associated with them, and also take advantage of being able to build nested attributes for each other. Can I add a tag in the model specify a call to .spouse should return this user? Or would it just be user.user?

Tony M
  • 241
  • 1
  • 4
  • 14
  • 1
    If you can include more information about your user model and users table structure it will help us answer your question as accurately as possible. – Dan Jan 31 '20 at 20:25
  • 1
    @Dan I don't currently have any relationships specified on the user model or other fields in the database for users. I'm only planning to have the one User model (not separate models for a user and a spouse). – Tony M Jan 31 '20 at 20:50

1 Answers1

2

In your user.rb model you can make an association like this:

class AdminUser < ApplicationRecord
  has_one :spouse, through: :user, foriegn_key: :spouse_id

This is assuming you have a field in your users table called spouse_id which is a foreign_key to users. (see below)

More information about this can be found here: Rails has_one :through association

When you add your spouse_id (or spouse_user_id might be a better name) via a migration don't forget to add a foreign key to strongly enforce legitimate data at the DB level.

add_column :users, :spouse_user_id, :integer
add_foreign_key :users, :users, column: 'spouse_user_id'

Example usage:

User id: 1, name: 'Fred', spouse_user_id: 2

User id: 2, name: 'Wilma', spouse_user_id: 1

User.find(1).spouse
=> Wilma

User.find(2).spouse
=> Fred
Dan
  • 1,238
  • 2
  • 17
  • 32
  • 1
    The linked Q&A states that `:through` "helps set up a one-to-one association with a third model by going through a second model". What is the second model here? – jvillian Jan 31 '20 at 20:36
  • Agreed with the comment above. I'm looking to set up an association from users to other users, so there is only one model in question here. @Dan, are you saying this will work with only the one user model and not having a separate model for spouse? – Tony M Jan 31 '20 at 20:42
  • Yes, it will work with only one user model. It's very similar to the challenge or having a one user report to another user in a business-type application. – Dan Jan 31 '20 at 20:52
  • Be careful if you don't want polygamy though. Nothing in the code above stops one user from being the spouse of several other users. – Dan Jan 31 '20 at 20:54
  • @Dan how do I make the relationship reciprocal though (so that one user's spouse automatically makes the other user their spouse also)? Essentially they need to have and belong to each other. – Tony M Jan 31 '20 at 21:08
  • 1
    `has_one` is implies a `belongs_to`. This https://stackoverflow.com/questions/3808926/whats-the-difference-between-belongs-to-and-has-one explains it pretty well I think. – Dan Jan 31 '20 at 21:22
  • This should be a belongs_to assocation since the foreign key is on this table. – max Feb 01 '20 at 05:25