ActiveRecord :: Enum – PG :: InvalidTextRepresentation:ERROR:整数的输入语法无效:
我有一个奇怪的错误,并希望有人能指出我正确的方向。 我有一个名为Organizations的模型,以及一个名为department
的属性,请参阅下面的模式摘录:
t.integer "department", default: 0
在我的模型里面已经为这个属性定义了我的枚举,因为我正在使用ActiveRecord :: Enum ,如下所示:
enum department: [:conferences, :design_teams, :services, :clubs, :events, :communications]
但是当我查询JobPosting.joins(job: :organization).where(organizations: { department: 'conferences' })
我收到的错误是:
PG::InvalidTextRepresentation: ERROR: invalid input syntax for integer: "conferences"
仅供参考:一个组织有很多乔布斯,乔布斯有很多JobPostings。
但是,当我查询Organization.where(department: 'conferences')
它可以工作。
任何帮助将不胜感激。
这适用于ror5。
JobPosting.joins(job: :organization).where(organizations: { department: Organization.departments['conferences'] })
我甚至不确定ror3中是否有enum
。
其他方法是设置基于文本的枚举。 在我看来,这是enum的最佳方式:
DEPARTMENTS_ENUM = [:conferences, :design_teams, :services, :clubs, :events, :communications] enum department: Hash[*DEPARTMENTS_ENUM.collect{|v| [v, v]}.flatten()]
部门列类型将更改后,它将起作用。
Organization.where(department: 'conferences')
也会工作