ActiveRecord :: Enum - PG :: InvalidTextRepresentation:ERROR:整数的输入语法无效:

时间:2017-03-28 23:33:49

标签: ruby-on-rails ruby ruby-on-rails-3 activerecord enums

我有一个奇怪的错误,并希望有人能指出我正确的方向。我有一个名为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')时,它就可以了。

非常感谢任何帮助。

3 个答案:

答案 0 :(得分:3)

这适用于ror5。

JobPosting.joins(job: :organization).where(organizations: 
{ department: Organization.departments['conferences'] }) 

我甚至不确定ror3中的enumavailable

答案 1 :(得分:1)

其他方法是设置基于文本的枚举。在我看来,这是枚举的最佳方式:

DEPARTMENTS_ENUM = [:conferences, :design_teams, :services, :clubs, :events, :communications]
enum department: Hash[*DEPARTMENTS_ENUM.collect{|v| [v, v]}.flatten()]

在部门列类型发生变化后,它将起作用。

Organization.where(department: 'conferences')

也会工作

答案 2 :(得分:0)

我没有直接回答问题,但是当我们搜索PG::InvalidTextRepresentation: ERROR: invalid input value for enum

时,它是google上的第一个链接

它可以帮助:

可以这样声明我们的枚举,这样,它将不会将字符串转换为整数,而是将其转为postgresql。

enum provider: { ig: "ig", fb: "fb", powertrack: "powertrack", gnip: "gnip" }