Has Many Through Association

 https://dev.to/neshaz/join-table-in-rails-23b5'


class User < ApplicationRecord
  has_many :user_roles
  has_many :roles, through: :user_roles
end

class UserRoles < ApplicationRecord
  belongs_to :user
  belongs_to :role
end

class Role < ApplicationRecord
  has_many :user_roles
  has_many :users, through: :user_roles
end






Comments

Popular posts from this blog

Rails 7 Features :: Comparison with Rails 6 and Rails 5