简体   繁体   中英

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

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.

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.

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

I am not directly answering the question but as it is the first link on google when we search PG::InvalidTextRepresentation: ERROR: invalid input value for enum

it can help:

it is possible to declare our enum like this, this way it will not convert the string to an integer but let this job to postgresql.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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