Leo Le
Leo Le

Reputation: 825

Enum for two class

I have two class Customer and Passenger (who in flight) with same attribute gender. So in the Customer class I declare enum gender

class Customer < ActiveRecord::Base
  enum GENDER: {MALE: 1, FEMALE: 2}
end

And this enum is also used by Passenger class.

What is the best practice in this case ? Should I split this enum to another class (for example: GenderHelper) ?

If yes, how can I declare enum in the helper class ? Inherit from ActiveRecord::Base like a model ?

Upvotes: 1

Views: 485

Answers (1)

Aetherus
Aetherus

Reputation: 8898

You can create a concern and include it in both Customer and Passenger.

app/models/concerns/gender.rb

module Gender
  extend ActiveSupport::Concern

  included do
    enum GENDER: {MALE: 1, FEMALE: 2}
  end
end

app/models/customer.rb

class Customer < ActiveRecord::Base
  include Gender
end

Upvotes: 2

Related Questions