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

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"

仅供参考:组织 has_many 职位和职位 has_many JobPostings。

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

如有任何帮助,我们将不胜感激。

这适用于 ror5。

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

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

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

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 上的第一个 link

它可以帮助:

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

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