Rails PG :: SyntaxError:ERROR澄清

时间:2017-08-20 23:56:45

标签: ruby-on-rails

我收到一个奇怪的错误。我有客户消息.... 客户has_many消息..和消息belongs_to客户 客户有电话列,并且消息中包含列。 以下代码假设获取当前客户ID并获取手机列,然后根据手机匹配的所有消息 >到列。然后使用客户ID更新所有消息。

我收到此错误,我没有看到问题。 有人可以告诉我我做错了吗?错误是在每个循环中触发的。我试图检查@foundmessage_all,但这不起作用。

 PG::SyntaxError: ERROR: syntax error at or near "to" LINE 1: SELECT
 "messages".* FROM "messages" WHERE (to = '2081234567'... ^ : SELECT
 "messages".* FROM "messages" WHERE (to = '2081234567' )

客户/ show.rb

 @customer = Customer.find(params[:id])
 tempphone = @customer.phone
 @foundmessage_all = Message.where('to = ? ', tempphone)
       if @foundmessage_all != nil
          @foundmessage_all.each do |t|
          t.update_attribute(:customer_id, @customer.id)
          end
       else
          #other stuff
       end

1 个答案:

答案 0 :(得分:1)

更改为以下声明:

@foundmessage_all = Message.where(to: tempphone)

如果这不能解决问题,请发布完整的堆栈跟踪。

另外,您应该检查present?上的@foundmessage_all而不是!= nil

if @foundmessage_all.present?