I’m creating a Rails application and I’m having trouble regarding ownership of a record.
I have a many to many relationship and I would like to find the owner/creator of a record.
Models :
class User < ApplicationRecord
has_many :user_accounts
has_many :accounts, through :user_accounts
end
class UserAccount < ApplicationRecord
belongs_to :user
belongs_to :account
end
class Account < ApplicationRecord
has_many :user_accounts
has_many :users, through :user_accounts
end
How could I find the User that actually created the Account ?
I’ve thought about adding a owner_id field in Account with a belongs_to relationship (e.g. : belongs_to :owner, class_name: 'User' ), but I wanted to know if there was any other way or better way to achieve that ?