user3311539
user3311539

Reputation:

Rails Model field value

I have a model by the name Estimation in Rails which has a field by the name request_type_id. RequestType is a model by itself. The possible values of the request_type field for all objects of Estimation class is fixed (medium, low, high).

The RequestType Model has an auto generated id and Name as the available fields. I am planning to insert the three values, low, medium and high in the model and these will have id's 1,2 and 3. So is the below class design correct?

class RfsEstimation < ActiveRecord::Base
has_one :request_type

Upvotes: 1

Views: 136

Answers (2)

Tashi Dendup
Tashi Dendup

Reputation: 359

If you want to keep fixed value for particular attributes then it is always good to define that particular column as enum

eg:- Add request_type as one of the column or attributes for RfsEstimation model with integer datatype.

class RfsEstimation < ActiveRecord::Base
  enum request_type: { low: 1, medium: 2, high: 3 }
end

Upvotes: 1

messanjah
messanjah

Reputation: 9278

Since RfsEstimation holds the foreign_key for RequestType, you'll want to use a belongs_to association.

class RfsEstimation < ActiveRecord::Base
belongs_to :request_type

Upvotes: 0

Related Questions