ruby-on-rails 如何在枚举类型中创建新的ActiveRecord记录?

n6lpvg4x  于 2022-11-19  发布在  Ruby
关注(0)|答案(1)|浏览(98)
class Degree < ApplicationRecord
  belongs_to :user

  enum :degree_type, {
    high_school: 'high_school',
    associate: 'associate',
    bachelor: 'bachelor',
    masters: 'masters',
    doctoral: 'doctoral'
  }, default: 'high_school'
end

u = User.last

u.degrees.degree_types

{"high_school"=>"high_school",
 "associate"=>"associate",
 "bachelor"=>"bachelor",
 "masters"=>"masters",
 "doctoral"=>"doctoral"}

工作正常..

尝试使用枚举创建新记录

第一个
没有错误,是否有效?

u.degrees
[]

不,不,不

u.update(degrees: :high_school)

other_array.each { |val| raise_on_type_mismatch!(val) }

如何在枚举类型中创建新的ActiveRecord记录?

wtlkbnrh

wtlkbnrh1#

我看不出在用户记录上使用update会创建一个新的学位记录的任何理由。

user.degrees.create(degree_type: Degree.degree_types[:high_school])

相关问题