Association to User using a key named approver_id

I KNOW this is probably very simple, but I am scratching my head trying to get it to work…

I have a table with a key named approver_id

What I want to do is associate this to the User class

So I can do Event.approver.first_name, etc…

in Event I have

has_one :approver, class: ‘User’

So how do I define the relationship to associate Event.approver_id to User.id

John

Use „class_name“, not „class“ as option key. Also, from the Model that holds the ID of the other Model, it’s always „belongs_to“.

So in Event: belongs_to :approver, class_name: „User“

and you’re good to go.

Sorry… I was using class_name: Typed it incorrectly here…

This worked for me…

has_one :approver, class_name: “User”, foreign_key: ‘id’, primary_key: ‘approver_id’

John

This should be much more easily done with belongs_to really…

has_one is used if the other model holds the id to this model. Which, if I understood correctly, isn’t the case in your code.