简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM