2016-05-06 139 views
2

準備済みの文をレールに使用して "IN"クエリを実行しています。 PG :: InvalidTextRepresentationエラーが表示されます。PG :: InvalidTextRepresentation:エラー:整数の入力構文が無効です

コード:

def mark_ineligible(match_ids) 
    ids = match_ids.join(", ") 
    result = epr("mark_matches_as_ineligible", 
       "UPDATE matches SET is_eligibile=false WHERE id IN ($1)", 
       [ids]) 
end 

def epr(statementname, statement, params) 
    connection = ActiveRecord::Base.connection.raw_connection 
    begin 
    result = connection.exec_prepared(statementname, params) 
    return result 
    rescue PG::InvalidSqlStatementName => e 
    begin 
     connection.prepare(statementname, statement) 
    rescue PG::DuplicatePstatement => e 
     # ignore since the prepared statement already exists 
    end 
    result = connection.exec_prepared(statementname, params) 
    return result 
    end 
end 

この使用を起動しようとしている:

match_ids = [42, 43] 
mark_ineligible match_ids 
PG::InvalidTextRepresentation: ERROR: invalid input syntax for integer: "42, 43" 

    from (irb):24:in `exec_prepared' 
    from (irb):24:in `rescue in epr' 
    from (irb):15:in `epr' 
    from (irb):8:in `mark_ineligible' 
    from (irb):35 

こちらを助けてください。私はなぜこのエラーが発生しているのか、それを修正する方法を知りたい。 、

def mark_ineligible(match_ids) 
    result = epr("mark_matches_as_ineligible", 
      "UPDATE matches SET is_eligibile=false WHERE id IN ($1)", match_ids) 
end 

としたときにmark_ineligibleを呼び出すを引数として配列を渡す:

おかげで、

+0

代わりに '[IDS]'渡すのでは、それが配列なので 'ids'を渡してみてください。 – dp7

+0

こんにちは、 "ids"はコンマで区切られた文字列です。私は最終的なクエリが "UP_ATEにマッチするように期待しています。SET is_eligibile = false WHERE id IN(42,43)" – anshul410

+0

@ ansul410 'ids = match_ids.join("、 ")' - これをあなたの 'mark_ineligible'メソッドから削除し、クエリに 'match_ids'を直接渡します。 – dp7

答えて

0

mark_ineligibleは、次のようになります

mark_ineligible(match_ids) #=>match_ids = [42,43] 
関連する問題