Forum: Ruby on Rails compare this sql query and corresponding ruby code

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
661ae6e97d0e16c2af2aaf7d197e8387?d=identicon&s=25 Sijo k g (sijo)
on 2009-05-13 12:47
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
661ae6e97d0e16c2af2aaf7d197e8387?d=identicon&s=25 Sijo k g (sijo)
on 2009-05-13 13:24
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
A91bd6cef23eb3516245a092e196c4da?d=identicon&s=25 Maurício Linhares (mauricio)
on 2009-05-13 13:28
(Received via mailing list)
Here's the correct sintax:

Activity.update_all( [ 'name = ?', 'hi' ] , ["ticket_id =?",1])


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



On Wed, May 13, 2009 at 8:24 AM, Sijo Kg
1e7782e67bb34c9c67ed19d5cde5f4eb?d=identicon&s=25 Tom Z Meinlschmidt (Guest)
on 2009-05-13 13:31
(Received via mailing list)
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  www.maxwellrender.cz  www.lightgems.cz
===============================================================================
661ae6e97d0e16c2af2aaf7d197e8387?d=identicon&s=25 Sijo k g (sijo)
on 2009-05-13 13:39
Hi
   Thanks for the reply.This working ut another question how can we
specify :conditions with update_all

Sijo
This topic is locked and can not be replied to.