On 10 Sep 2008, at 17:04, Maurício Linhares wrote:

>
> You should be doing it like this:
>
> Temp.increment_counter( :counter, id )
>
> This will increment the property ":counter" for the model Temp with
> "id", this column must have a default value of 0 (zero) or else this
> call won't work.

this works because it does

update foos set counter = counter + 1

and then it's the databases problem to worry about concurrent queries  
and so on.
If what you showed was a simplification of what you're actually doing  
(and you can't reduce it to increment_counter) then you'll need to  
look into locking (either optimistic or pessimistic)


Fred
>
>
> On Wed, Sep 10, 2008 at 12:59 PM, Difei Zhao
> <[EMAIL PROTECTED]> wrote:
>>
>> Greetings all,
>>
>> I have a model which holds a counter field, the field have to be
>> incremented frequently to record the access frequency of the specific
>> object:
>>
>> @obj = Temp.find(id)
>> @obj.counter += 1
>> @obj.save
>>
>> The implementation seems quite simple, but I am worrying about the
>> race condition (if there are any, I do not know whether rails is
>> thread-safe) since these statements may be interleaved. Could anyone
>> help about this? Thanks very much!
>>
>> Difei
>> --
>> Posted via http://www.ruby-forum.com/.
>>
>>>
>>
>
>
>
> -- 
> Maurício Linhares
> http://alinhavado.wordpress.com/ (pt-br) | http:// 
> blog.codevader.com/ (en)
> João Pessoa, PB, +55 83 8867-7208
>
> >


--~--~---------~--~----~------------~-------~--~----~
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 [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to