0

I'm getting a strange error. I have Customers and Messages.... Customers has_many Messages.. and message belongs_to customer The customer has a phone column and a message has a to column. The code below is suppose to take the current customer id and grab the phone column then find all messages that match the phone based on the to column. Then update all the messages with the customer id.

I'm getting this error that I don't see the problem. Can someone tell me what I'm doing wrong? The error is triggered on the each loop. I tried to inspect @foundmessage_all but that doesn't work.

 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' )

customer/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 Answer 1

1

Change to below statement:

@foundmessage_all = Message.where(to: tempphone)

If this doesn't solve the issue, post the full stack trace.

Also you should be checking present? on @foundmessage_all instead of != nil

if @foundmessage_all.present?
Sign up to request clarification or add additional context in comments.

3 Comments

I'm getting the same error. I'm a little new to rails. How can I get the full stack trace?
Ok I changed the 3rd line to @foundmessage_all = Message.where(:to => tempphone) This version works.
Oh, that might be because of ruby version being older than 1.9, pl mark it as answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.