# Update issue

**URL:** <https://www.ruby-forum.com/t/update-issue/133088>\
**Category:** Rails\
**Created:** [March 27, 2008, 4:38pm UTC](https://www.ruby-forum.com/t/update-issue/133088 "2008-03-27T16:38:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [March 27, 2008, 4:38pm UTC](https://www.ruby-forum.com/t/update-issue/133088/1 "2008-03-27T16:38:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [March 27, 2008, 4:51pm UTC](https://www.ruby-forum.com/t/update-issue/133088/2 "2008-03-27T16:51:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [March 27, 2008, 5:01pm UTC](https://www.ruby-forum.com/t/update-issue/133088/3 "2008-03-27T17:01:58Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [March 27, 2008, 5:22pm UTC](https://www.ruby-forum.com/t/update-issue/133088/4 "2008-03-27T17:22:58Z")

</div>

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

> [http://softiesonrails.com/2007/7/23/performing-a-sql-update-in-rails](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

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [March 27, 2008, 5:30pm UTC](https://www.ruby-forum.com/t/update-issue/133088/5 "2008-03-27T17:30:40Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [March 27, 2008, 5:31pm UTC](https://www.ruby-forum.com/t/update-issue/133088/6 "2008-03-27T17:31:51Z")

</div>

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/wall/trakb/10-Long)  
[http://FATdrive.tv/](http://FATdrive.tv/) - store, play, share

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [March 27, 2008, 5:35pm UTC](https://www.ruby-forum.com/t/update-issue/133088/7 "2008-03-27T17:35:00Z")

</div>

Thanks guys!

(I lost my afternoon, but you saved my evening… 🙂
