There is a model called Student and has an attribute called :studies_level
.
有一个名为Student的模型,它有一个名为:studies_level的属性。
studies_level
can have one of the following values: ['school_graduate', 'undergraduate', 'graduate', 'postgraduate', 'doctoral', 'postdoctoral']
studies_level可以具有以下价值之一:['school_graduate','本科','研究生','研究生','博士','博士后']
There is also another model called University
还有另一种叫做大学的模型
If :studies_level
is postgraduate then Student must fill the following attributes :undergraduate_university
and :postgraduate_university
with a University_id
如果:studies_level是研究生,那么学生必须填写以下属性:本科_大学和:University_idiversity with a University_id
Is there any way to achieve this with Rails model relations (belongs_to, etc) ?
有没有办法用Rails模型关系(belongs_to等)来实现这个目的?
1 个解决方案
#1
0
belongs_to :undergraduate_university, class_name: :University
belongs_to :postgraduate_university, class_name: :University
validates :undergraduate_university, presence: true, if: :studies_level=='postgrad'
validates :postgraduate_university, presence: true, if: :studies_level=='postgrad'
validates_associated :undergraduate_university
validates_associated :postgraduate_university
#1
0
belongs_to :undergraduate_university, class_name: :University
belongs_to :postgraduate_university, class_name: :University
validates :undergraduate_university, presence: true, if: :studies_level=='postgrad'
validates :postgraduate_university, presence: true, if: :studies_level=='postgrad'
validates_associated :undergraduate_university
validates_associated :postgraduate_university