ActiveRecord::StatementInvalid: PG InFailedSqlTransaction

后端 未结 10 2088
礼貌的吻别
礼貌的吻别 2020-12-13 01:33

I am trying to create an ActiveRecord Object.But I\'m getting this error while creating it.

(0.1ms)  ROLLBACK
ActiveRecord::StatementInvalid: PG::InFailedSql         


        
10条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-13 02:21

    I got that problem. And I found out that it was my query. It mean when I query with association without specifying a table column. ex:

    class Holiday < ApplicationRecord
         belongs_to :company
    end
    
    class Company < ApplicationRecord
        has_many :timeoffs
    end
    

    In Holiday model I query

    company.timeoffs.where("(start_date <= ? and end_date >= ?) and id != ?", begin_date, begin_date, 1)
    

    The error occurs because I didn't specify which table's id It worked for me after I changed the code to

    company.timeoffs.where("(start_date <= ? and end_date >= ?) and time_offs.id != ?", begin_date, begin_date, 1)
    

提交回复
热议问题