ActiveRecord::Base.connection.execute is exec?

344 views
Skip to first unread message

tamagohan

unread,
Dec 3, 2011, 9:59:54 PM12/3/11
to Ruby on Rails: Talk
Hi all, I update column by 'ActiveRecord::Base.connection.execute'
ActiveRecord::Base.connection.execute "UPDATE requests SET count=count
+10 WHERE id=1 AND count <= 1000"
But, I can't know if condition,..'count <= 1000' is met
How can I know if condtion is met?

Please in mind, any other process very often update count, not only
add but also subtract

appreciate any help.
Thank you
Masaki.

Frederick Cheung

unread,
Dec 4, 2011, 3:18:09 PM12/4/11
to Ruby on Rails: Talk

On Dec 4, 2:59 am, tamagohan <takahashi...@gmail.com> wrote:
> Hi all, I update column by 'ActiveRecord::Base.connection.execute'
> ActiveRecord::Base.connection.execute "UPDATE requests SET count=count
> +10 WHERE id=1 AND count <= 1000"
> But, I can't know if condition,..'count <= 1000' is met
> How can I know if condtion is met?
>

I think that if you use update rather than execute you'll get the
number of modified rows returned.

Fred

tamagohan

unread,
Dec 4, 2011, 7:59:12 PM12/4/11
to Ruby on Rails: Talk
On 12月5日, 午前5:18, Frederick Cheung <frederick.che...@gmail.com> wrote:
> On Dec 4, 2:59 am, tamagohan <takahashi...@gmail.com> wrote:> Hi all, I update column by 'ActiveRecord::Base.connection.execute'
> > ActiveRecord::Base.connection.execute "UPDATE requests SET count=count
> > +10 WHERE id=1 AND count <= 1000"
> > But, I can't know if condition,..'count <= 1000' is met
> > How can I know if condtion is met?
>
> I think that if you use update rather than execute you'll get the
> number of modified rows returned.
>
> Fred
>

Thanks!
but sorry...

I want to use execute.
Because, I feared race condition.
For example,
process A get count,...100
process B get count,...100
process A update count to (count + 10),...110
process B update count to (count + 10),...110
But, after 2 proceess, count should be 120.

So, I update count by database, not by application.

Masaki

Frederick Cheung

unread,
Dec 5, 2011, 6:09:19 AM12/5/11
to Ruby on Rails: Talk

On Dec 5, 12:59 am, tamagohan <takahashi...@gmail.com> wrote:
> On 12月5日, 午前5:18, Frederick Cheung <frederick.che...@gmail.com> wrote:
>
> > On Dec 4, 2:59 am, tamagohan <takahashi...@gmail.com> wrote:> Hi all, I update column by 'ActiveRecord::Base.connection.execute'
> > > ActiveRecord::Base.connection.execute "UPDATE requests SET count=count
> > > +10 WHERE id=1 AND count <= 1000"
> > > But, I can't know if condition,..'count <= 1000' is met
> > > How can I know if condtion is met?
>
> > I think that if you use update rather than execute you'll get the
> > number of modified rows returned.
>
> > Fred
>
> Thanks!
> but sorry...
>
> I want to use execute.

You've misunderstood me. I meant se
ActiveRecord::Base.connection.update "..."
which is the same as update except that you get the altered row count
(and also clears rails' query cache)

Fred

高橋正樹

unread,
Dec 5, 2011, 9:03:08 AM12/5/11
to rubyonra...@googlegroups.com

Thanks, Fred!
I misunderstood..

I could get affecyed colmun count.
Thank you so much!

Masaki

2011/12/05 20:09 "Frederick Cheung" <frederic...@gmail.com>:
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply all
Reply to author
Forward
0 new messages