Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Wiley Conte
Wiley Conte
9,618 Points

What is the best way to handle Rails active record associations between different user roles (enum in User model)?

I'm setting up a user model with three roles:

class User < ApplicationRecord

  enum role: {
    producer: 0,
    member: 1,
    admin: 2,]

  after_initialize do
    if self.new_record?
      self.role ||= :member
    end
  end

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  validates :email, :uniqueness => {:case_sensitive => false} 

I'm wondering how I can access these roles for associations with my other Models? For example in one model shares needs to belongs_to :producer and has_many :members but this doesn't seem to work properly. Thank you in advance!