Update issue

Hi there,

can anyone please tell me what’s wrong with the following line? (It must
be so simple but I’ve been trying to find the error for so long…)

Message.update_all (:read => true, 'target_id =

#{session[:user_id]}’)

This is supposed to set the attribute “read” to “true” (=1) where the
attribute “target_id” is equal to the user_id stored in the current
session.

The error that I get is:

.../app/controllers/message_controller.rb:4: syntax error,

unexpected ‘)’, expecting tASSOC

Thank you very much!
Tom

On 27 Mar 2008, at 15:38, Tom Ha wrote:

This is supposed to set the attribute “read” to “true” (=1) where the
attribute “target_id” is equal to the user_id stored in the current
session.

Looks like you want

Message.update_all (:read => true, :target_id => session[:user_id])

Fred

Not really, because this would update NOT ONLY the :read attribute but
ALSO the :target_id attribute (which is not what I want…).

What I want it to do: update ONLY :read WHERE :target_id =
session[:user_id]

Can you spot the error in my statement above?

(I got some instructions from this page:
http://softiesonrails.com/2007/7/23/performing-a-sql-update-in-rails)

Frederick C. wrote:

On 27 Mar 2008, at 15:38, Tom Ha wrote:

This is supposed to set the attribute “read” to “true” (=1) where the
attribute “target_id” is equal to the user_id stored in the current
session.

Looks like you want

Message.update_all (:read => true, :target_id => session[:user_id])

Fred

On 27 Mar 2008, at 16:01, Tom Ha wrote:

http://softiesonrails.com/2007/7/23/performing-a-sql-update-in-rails)

Oops. wasn’t following closely enough. The basic issue is that you’re
not allowed to drop the {} for a hash if there are normal parameters
coming after it.

Fred

Tom Ha wrote:

Message.update_all (:read => true, 'target_id =

#{session[:user_id]}’)

Also note that #{} doesn’t interpolate in single quotes, so you probably
want to use double quotes here…

Tom Ha writes

Hi there,

can anyone please tell me what’s wrong with the following line? (It
must
be so simple but I’ve been trying to find the error for so long…)

Message.update_all (:read => true, 'target_id =

#{session[:user_id]}’)

I think you want #{session[:user_id} to resolve before the SQL update
so you need to change the single quote to double ("). Like

Message.update_all (:read => true, “target_id = #{session[:user_id]}”)

even better yet,

Message.update_all (:read => true, “target_id =
‘#{session[:user_id]}’”)

– Long
http://FATdrive.tv/wall/trakb/10-Long
http://FATdrive.tv/ - store, play, share

Thanks guys!

(I lost my afternoon, but you saved my evening… :slight_smile:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs