【问题标题】:ActiveRecord::Enum - PG::InvalidTextRepresentation: ERROR: invalid input syntax for integer:ActiveRecord::Enum - PG::InvalidTextRepresentation:错误:整数输入语法无效:
【发布时间】:2017-03-28 23:33:48
【问题描述】:

我遇到了一个奇怪的错误,希望有人能指出我正确的方向。我有一个名为 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') 时,它可以工作。

任何帮助将不胜感激。

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 activerecord enums


    【解决方案1】:

    这适用于 ror5。

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

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

    【讨论】:

    • 在 RoR 3 中枚举不可用。
    【解决方案2】:

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

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

    在部门栏类型发生变化后生效。

    Organization.where(department: 'conferences')
    

    也可以

    【讨论】:

      【解决方案3】:

      我不是直接回答这个问题,而是因为它是我们搜索PG::InvalidTextRepresentation: ERROR: invalid input value for enum时谷歌上的第一个链接

      它可以提供帮助:

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

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

      【讨论】:

        猜你喜欢
        • 2018-10-22
        • 1970-01-01
        • 1970-01-01
        • 2016-10-12
        • 2013-08-20
        • 2014-03-30
        • 1970-01-01
        • 2014-07-17
        • 2012-12-21
        相关资源
        最近更新 更多