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

On 16 June 2011 21:41, Walter Lee Davis <wa...@wdstudio.com> wrote:

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

On 16 June 2011 21:36, Walter Lee Davis <wa...@wdstudio.com> wrote:

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

On 16 June 2011 21:24, Walter Lee Davis <wa...@wdstudio.com> wrote:

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?

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...........

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


--
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 rubyonrails- t...@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com . For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en .


--
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 rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to