That is true. I'll probably change this.
This doesn't solve my problem though. I realize that I phrased myself a bit odd in my first post I will clarify below.
I have three models, User, Entry, Company
A user belongs to a company
An Entry belongs to the User who creates it.
An entry can also be assigned to another user (think of it like a task). So, I have these relationships in Entry:
belongs_to :user
belongs_to :assigned_user, class_name: "User"
The :assigned_user relationship should only be to a user who is related to the same company as :user. I validate it like this:
def assigned_user_must_be_in_company
if assigned_user.present? && assigned_user.company_id != user.company_id
errors.add(:assigned_user, "användaren måste tillhöra samma företag")
end
end
Which works OK.
But it would be nice to also have a constraint for this on the relation itself to get the correct users automatically etc.