Randz67
Randz67

Reputation: 907

Two models with 2 the same relationship with each other in RAILS 5

Here's the scenario:

An HMO has_many USER (these are the members of the HMO). A User belongs to HMO.

A USER ==Owner of HMO== has_one HMO. HMO belongs_to USER.

Now, when I query Hmo.first.users the OWNER is also included in the list. How can I implement this on RAILS ActiveRecord relationship?

Upvotes: 0

Views: 86

Answers (1)

MrShemek
MrShemek

Reputation: 2483

What is the difference between user and owner of HMO?
If you have a boolean flag for the owner, then you can use something like this to get users without owner:

has_many :users, -> { where owner: false }, class_name: 'User'

Documentation: Scopes for has_many

Upvotes: 0

Related Questions