繁体   English   中英

ActiveRecord::Enum - PG::InvalidTextRepresentation:错误:整数输入语法无效:

[英]ActiveRecord::Enum - PG::InvalidTextRepresentation: ERROR: invalid input syntax for integer:

我有一个奇怪的错误,希望有人能指出我正确的方向。 我有一个名为 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"

仅供参考:一个组织有_many Jobs,而Job has_many JobPostings。

但是当我查询Organization.where(department: 'conferences')它工作。

任何帮助将不胜感激。

这适用于 ror5。

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

我什至不确定enum在 ror3 中是否可用

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

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

它会在部门列类型更改后起作用。

Organization.where(department: 'conferences')

也会工作

我没有直接回答这个问题,但是当我们搜索PG::InvalidTextRepresentation: ERROR: invalid input value for enum时,它是 google 上的第一个链接

它可以帮助:

可以像这样声明我们的枚举,这样它就不会将字符串转换为整数,而是让这个工作交给 postgresql。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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