I am trying to setup my rails models where I have a User and they have many addresses but one is their primary address. I want to be able to access it like so, User.first.primary_address_id and also User.first.primary_address to get the associated Address model.
Is there a “rails way” to set this up?
I know I could create a primary_address_id field for User and populate it in an AR callback. But that wouldn’t let me do something like User.first.primary_address
Or can I create associations as “User has_many Addresses” as well as “User has_one Address”?? or something along those lines?
This is the correct way to do it.
would return the first user’s primary address if they had one or nil if they didn’t.