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

I'm having a weird error and I was hoping someone could point me in the right direction. I have a model called "Organizations" and an attribute department

, see an excerpt from the diagram below:

t.integer  "department",  default: 0

      

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

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

      

But when I ask, JobPosting.joins(job: :organization).where(organizations: { department: 'conferences' })

I get an error:

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

FYI: Organization has_many Jobs and Job has_many JobPostings.

But when I ask Organization.where(department: 'conferences')

, it works.

Any help would be greatly appreciated.

0


source to share


2 answers


This works for ror5.

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

      



I'm not even sure what enum

was available in ror3.

+2


source


Another way is to install text enums. This is the best way to enumerate in my opinion:

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

      

It will work after changing the column type.



Organization.where(department: 'conferences')

      

Will work too

0


source







All Articles