If you do that and two users come along at the same time and make new
objects then they will both get the same number. Can you not just use
a before_save filter to set the field? If you think not can you
explain what you are trying to do (and why) in more detail as there is
almost certainly a better solution.
Colin
Please don't *ever* do this.
If you want a count of all the objects in a table, use
"ModeName.count". Using ".all.count" will instantiate an array of all
of the objects just to do a count of the array... this could be very
slow with a big table.
I think actually he asked about something counter-caching solution for stand-alone model/table. Good question, for the best approach it has to be realized on a database level. The second approach is to build additional caching table to keep counter values for anything you want. The last one is not so elegant.
Imagine 'create' action.
During the period from the instance is initialized:
@user = User.new(params[:user])
to the moment it saved
@user.save
anyone else can create user object(s), so your @user will be saved with incorrect counter value.
> What I am trying to do is have a *unique value* set in each new record
See: http://rubygems.org/gems/uuid
HTH!
--
Hassan Schroeder ------------------------ hassan.s...@gmail.com
http://about.me/hassanschroeder
twitter: @hassan
On Wed, Feb 8, 2012 at 8:54 PM, Bob Smith <bsm...@gmail.com> wrote:
> What I am trying to do is have a *unique value* set in each new record
See: http://rubygems.org/gems/uuid
Global variables?
For the love of all that's OO, at least hide it behind a singleton :-/
*shakes head*