# Compare this sql query and corresponding ruby code

**URL:** <https://www.ruby-forum.com/t/compare-this-sql-query-and-corresponding-ruby-code/167864>\
**Category:** Rails\
**Created:** [May 13, 2009, 12:47pm UTC](https://www.ruby-forum.com/t/compare-this-sql-query-and-corresponding-ruby-code/167864 "2009-05-13T12:47:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [May 13, 2009, 12:47pm UTC](https://www.ruby-forum.com/t/compare-this-sql-query-and-corresponding-ruby-code/167864/1 "2009-05-13T12:47:47Z")

</div>

Hi  
Suppose I have a two models Ticket and Activity

Ticket has\_many activities  
Activity belongs\_to Ticket

```
Now suppose if the Activity has field name Now for a Ticket with

```

id=1 say there are 10 activities So to update name of all activities I  
can directly wite the sql statement

update activities set name=‘somename’ where ticket\_id=1

```
So I am writing this like

```

ticket = Ticket.find(1)  
ticket.activities.each do |a|  
a.update\_attribute(:name =\> ‘somename’)  
end  
But what about the performance Is both the above query and the ruby  
code has same performance What is actually the generated sql for the  
ruby code  
Or Am I wrong? Is there any other way of doing this same like sql  
above?

Thanks in advance  
Sijo

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [May 13, 2009, 1:24pm UTC](https://www.ruby-forum.com/t/compare-this-sql-query-and-corresponding-ruby-code/167864/2 "2009-05-13T13:24:22Z")

</div>

Hi  
I tried like  
Activity.update\_all(“name=‘hi’”, :conditions =\> [“ticket\_id =?”,1])

But getting error  
ActiveRecord::StatementInvalid: RuntimeError: ERROR C42703 Mcolumn  
activities.conditions does not exist P42 Fparse\_func.c  
L1104 Runknown\_attribute: UPDATE “activities” SET name=‘hi’ WHERE  
(“activities”.“conditions” IN (E’ticket\_id =?’,1))  
Could you please correct the syntax  
Sijo

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [May 13, 2009, 1:28pm UTC](https://www.ruby-forum.com/t/compare-this-sql-query-and-corresponding-ruby-code/167864/3 "2009-05-13T13:28:52Z")

</div>

Here’s the correct sintax:

Activity.update\_all( [‘name = ?’, ‘hi’] , [“ticket\_id =?”,1])

- 

Maurício Linhares  
[http://alinhavado.wordpress.com/](http://alinhavado.wordpress.com/) (pt-br) | [http://blog.codevader.com/](http://blog.codevader.com/)  
(en)

On Wed, May 13, 2009 at 8:24 AM, Sijo Kg

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [May 13, 2009, 1:39pm UTC](https://www.ruby-forum.com/t/compare-this-sql-query-and-corresponding-ruby-code/167864/4 "2009-05-13T13:39:52Z")

</div>

Hi  
Thanks for the reply.This working ut another question how can we  
specify :conditions with update\_all

Sijo

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [May 13, 2009, 1:31pm UTC](https://www.ruby-forum.com/t/compare-this-sql-query-and-corresponding-ruby-code/167864/5 "2009-05-13T13:31:14Z")

</div>

hi,

try to look to your development log, u’ll see sql queries there.

and you might try this:

ticket = Ticket.find(1, :include =\> [:activity])

and see your query again

tom

Sijo Kg wrote:

> update activities set name=‘somename’ where ticket\_id=1  
> Or Am I wrong? Is there any other way of doing this same like sql  
> above?
> 
> Thanks in advance  
> Sijo

# –

Tomas Meinlschmidt, MS {MCT, MCP+I, MCSE, AER}, NetApp Filer/NetCache

- experienced RoR/PHP freelancer, available for hire

# [www.meinlschmidt.com](http://www.meinlschmidt.com) www.maxwellrender.cz www.lightgems.cz
