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

对着背影说爱祢 提交于 2019-12-12 05:11:10

问题


I am having a strange error and was hoping someone could point me in the right direction. I have a model called Organizations, and a attribute called department, see the excerpt from the schema below:

t.integer  "department",  default: 0

Inside my model have defined my enums for this attribute, as I am using ActiveRecord::Enum, like below:

enum department: [:conferences, :design_teams, :services, :clubs, :events, :communications]

But when I query, JobPosting.joins(job: :organization).where(organizations: { department: 'conferences' }) I get an error that reads:

PG::InvalidTextRepresentation: ERROR: invalid input syntax for integer: "conferences"

FYI: An Organization has_many Jobs, and Job has_many JobPostings.

But when I query Organization.where(department: 'conferences') it works.

Any help would be greatly appreciated.


回答1:


This works on ror5.

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

I'm not even sure if enum was available in ror3.




回答2:


Other way is to set text-based enums. In my opinion it is the best way for enum:

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

It will work after department column type will change.

Organization.where(department: 'conferences')

Will work too



来源:https://stackoverflow.com/questions/43081716/activerecordenum-pginvalidtextrepresentation-error-invalid-input-syntax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!