Set a progressive number

I want to assign a progressive number to an attribute.
I think that I can do this in the model

class Model < ActiveRecord::Base
before_validation(:on => :create) do
attribute = Model.count + 1

But there is no method count for Model.
I think I’m doing wrong.

On Jun 16, 2011, at 4:15 PM, Mauro wrote:

I want to assign a progressive number to an attribute.
I think that I can do this in the model

class Model < ActiveRecord::Base
before_validation(:on => :create) do
attribute = Model.count + 1

But there is no method count for Model.

Is Model just an example, or did you use that as your model name?
There is definitely the count method on this page:

http://ar.rubyonrails.org/classes/ActiveRecord/Calculations/ClassMethods.html#M000292

Walter

On 16 June 2011 21:24, Walter Lee D. [email protected] wrote:

But there is no method count for Model.

Is Model just an example, or did you use that as your model name?

The real code is:

class FirePrevention < ActiveRecord::Base
default_scope :order => ‘practice_number ASC’
before_validation(:on => :create) do
practice_number = self.count + 1
end

It says undefined method count…

On 16 June 2011 21:36, Walter Lee D. [email protected] wrote:

class Model < ActiveRecord::Base
default_scope :order => ‘practice_number ASC’
before_validation(:on => :create) do
practice_number = self.count + 1
end

It says undefined method count…

You may be getting a funny self there, then. Try this instead:

practice_number = FirePrevention.count + 1

It’s the same, same error.

On Jun 16, 2011, at 5:32 PM, Mauro wrote:

end

It says undefined method count…

You may be getting a funny self there, then. Try this instead:

practice_number = FirePrevention.count + 1

Walter

On Jun 16, 2011, at 5:39 PM, Mauro wrote:

class FirePrevention < ActiveRecord::Base

It’s the same, same error.

Have you tried this in rails console? In a new blank project, maybe?

Walter

On Jun 16, 2011, at 5:40 PM, Mauro wrote:

I think that I can do this in the model

practice_number = FirePrevention.count + 1

It’s the same, same error.

But why I must call FirePrevention while I am inside FirePrevention,
that’s why I use self.

Well, just to see if self was borked at that point somehow. I recall
reading very recently that self could act funny in before filters.

Walter

On 16 June 2011 21:39, Mauro [email protected] wrote:

class FirePrevention < ActiveRecord::Base

It’s the same, same error.

But why I must call FirePrevention while I am inside FirePrevention,
that’s why I use self.

On 16 June 2011 21:41, Walter Lee D. [email protected] wrote:

You may be getting a funny self there, then. Try this instead:

practice_number = FirePrevention.count + 1

It’s the same, same error.

Have you tried this in rails console? In a new blank project, maybe?

That work if I call FirePrevention.count from a controller.

On Jun 16, 2011, at 5:46 PM, Mauro wrote:

It says undefined method count…

You may be getting a funny self there, then. Try this instead:

practice_number = FirePrevention.count + 1

It’s the same, same error.

Have you tried this in rails console? In a new blank project, maybe?

That work if I call FirePrevention.count from a controller.

That actually makes a ton of sense, since the Model has no notion of
the collection of sibling objects, only the Empyrean ideal of the
Model that spawned them. But count is a property of a collection of
unique objects.

Walter

On 16 June 2011 21:50, Walter Lee D. [email protected] wrote:

On 16 June 2011 21:24, Walter Lee D. [email protected] wrote:

But there is no method count for Model.

That work if I call FirePrevention.count from a controller.

That actually makes a ton of sense, since the Model has no notion of the
collection of sibling objects, only the Empyrean ideal of the Model that
spawned them. But count is a property of a collection of unique objects.

So the undefined method count rised by the model is correct?
How about a method to assign a progressive number to
FirePrevention.practice_number?

Does everyone here realize that the ‘id’ is typically tied to a
database column that is set to auto-increment (in whatever fashion the
underlying database supports) for the primary key?

Also, if you ever delete a record, the .count will decrease and you’ll
get a duplicate for the next created record.

Perhaps you want FirePrevention.max(&:id)+1 if you can’t just rely on
the id directly.

-Rob

On Jun 16, 2011, at 5:53 PM, Mauro wrote:

On Jun 16, 2011, at 5:32 PM, Mauro wrote:

before_validation(:on => :create) do
default_scope :order => ‘practice_number ASC’
It’s the same, same error.
that
To post to this group, send email to rubyonrails-
[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
.

Rob B.
[email protected] http://AgileConsultingLLC.com/
[email protected] http://GaslightSoftware.com/

On Jun 16, 2011, at 3:12 PM, Rob B. wrote:

Does everyone here realize that the ‘id’ is typically tied to a database column
that is set to auto-increment (in whatever fashion the underlying database
supports) for the primary key?

Also, if you ever delete a record, the .count will decrease and you’ll get a
duplicate for the next created record.

Perhaps you want FirePrevention.max(&:id)+1 if you can’t just rely on the id
directly.

And even with the above you’ve got race conditions…

On Jun 16, 2011, at 5:53 PM, Mauro wrote:

On Jun 16, 2011, at 5:32 PM, Mauro wrote:

before_validation(:on => :create) do
default_scope :order => ‘practice_number ASC’
It’s the same, same error.
that
spawned them. But count is a property of a collection of unique
objects.

So the undefined method count rised by the model is correct?
How about a method to assign a progressive number to
FirePrevention.practice_number?

Yes, it’s apparently mixed into the controller, so you would have to
do that there. I could see you adding this to the create method
directly, or you could put it in a before_save there, it should work
fine. Probably exactly the same code as your initial example.

Walter

On 16 June 2011 22:12, Rob B. [email protected]
wrote:

Does everyone here realize that the ‘id’ is typically tied to a database
column that is set to auto-increment (in whatever fashion the underlying
database supports) for the primary key?

Also, if you ever delete a record, the .count will decrease and you’ll get a
duplicate for the next created record.

I had not thought about this.

You want to use self.class.count.

Self is a reference to the object it’s self (in your case, an instance
of FirePrevention), self.class is reference to the class.

On 17 June 2011 02:44, arai [email protected] wrote:

You want to use self.class.count.

But if I use self.maximum(:id) it works, there is no need to use
self.class.maximun(:id).
Why?

I have a multi-tenant system (by subdomain) and each subdomain needs
their own perspective on an incrementing check number. This does the
job:

before_validation(:on => :create) do
Check.transaction do
self.number = (Check.maximum(:number) || 0) + 1
end
end