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. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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> I think I''m doing wrong. > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > . > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 16 June 2011 21:24, Walter Lee Davis <waltd-HQgmohHLjDZWk0Htik3J/w@public.gmane.org> 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 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jun 16, 2011, at 5:32 PM, Mauro wrote:> On 16 June 2011 21:24, Walter Lee Davis <waltd-HQgmohHLjDZWk0Htik3J/w@public.gmane.org> 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 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- > talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > . > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 16 June 2011 21:36, Walter Lee Davis <waltd-HQgmohHLjDZWk0Htik3J/w@public.gmane.org> wrote:> > On Jun 16, 2011, at 5:32 PM, Mauro wrote: > >> On 16 June 2011 21:24, Walter Lee Davis <waltd-HQgmohHLjDZWk0Htik3J/w@public.gmane.org> 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 + 1It''s the same, same error. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 16 June 2011 21:39, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 16 June 2011 21:36, Walter Lee Davis <waltd-HQgmohHLjDZWk0Htik3J/w@public.gmane.org> wrote: >> >> On Jun 16, 2011, at 5:32 PM, Mauro wrote: >> >>> On 16 June 2011 21:24, Walter Lee Davis <waltd-HQgmohHLjDZWk0Htik3J/w@public.gmane.org> 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.But why I must call FirePrevention while I am inside FirePrevention, that''s why I use self. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jun 16, 2011, at 5:39 PM, Mauro wrote:> On 16 June 2011 21:36, Walter Lee Davis <waltd-HQgmohHLjDZWk0Htik3J/w@public.gmane.org> wrote: >> >> On Jun 16, 2011, at 5:32 PM, Mauro wrote: >> >>> On 16 June 2011 21:24, Walter Lee Davis <waltd-HQgmohHLjDZWk0Htik3J/w@public.gmane.org> 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? 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- > talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > . > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jun 16, 2011, at 5:40 PM, Mauro wrote:> On 16 June 2011 21:39, Mauro <mrsanna1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> On 16 June 2011 21:36, Walter Lee Davis <waltd-HQgmohHLjDZWk0Htik3J/w@public.gmane.org> wrote: >>> >>> On Jun 16, 2011, at 5:32 PM, Mauro wrote: >>> >>>> On 16 June 2011 21:24, Walter Lee Davis <waltd-HQgmohHLjDZWk0Htik3J/w@public.gmane.org> 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. > > 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> > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > . > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 16 June 2011 21:41, Walter Lee Davis <waltd-HQgmohHLjDZWk0Htik3J/w@public.gmane.org> wrote:> > On Jun 16, 2011, at 5:39 PM, Mauro wrote: > >> On 16 June 2011 21:36, Walter Lee Davis <waltd-HQgmohHLjDZWk0Htik3J/w@public.gmane.org> wrote: >>> >>> On Jun 16, 2011, at 5:32 PM, Mauro wrote: >>> >>>> On 16 June 2011 21:24, Walter Lee Davis <waltd-HQgmohHLjDZWk0Htik3J/w@public.gmane.org> 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. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jun 16, 2011, at 5:46 PM, Mauro wrote:> On 16 June 2011 21:41, Walter Lee Davis <waltd-HQgmohHLjDZWk0Htik3J/w@public.gmane.org> wrote: >> >> On Jun 16, 2011, at 5:39 PM, Mauro wrote: >> >>> On 16 June 2011 21:36, Walter Lee Davis <waltd-HQgmohHLjDZWk0Htik3J/w@public.gmane.org> wrote: >>>> >>>> On Jun 16, 2011, at 5:32 PM, Mauro wrote: >>>> >>>>> On 16 June 2011 21:24, Walter Lee Davis <waltd-HQgmohHLjDZWk0Htik3J/w@public.gmane.org> >>>>> 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- > talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > . > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 16 June 2011 21:50, Walter Lee Davis <waltd-HQgmohHLjDZWk0Htik3J/w@public.gmane.org> wrote:> On Jun 16, 2011, at 5:46 PM, Mauro wrote: > >> On 16 June 2011 21:41, Walter Lee Davis <waltd-HQgmohHLjDZWk0Htik3J/w@public.gmane.org> wrote: >>> >>> On Jun 16, 2011, at 5:39 PM, Mauro wrote: >>> >>>> On 16 June 2011 21:36, Walter Lee Davis <waltd-HQgmohHLjDZWk0Htik3J/w@public.gmane.org> wrote: >>>>> >>>>> On Jun 16, 2011, at 5:32 PM, Mauro wrote: >>>>> >>>>>> On 16 June 2011 21:24, Walter Lee Davis <waltd-HQgmohHLjDZWk0Htik3J/w@public.gmane.org> 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.So the undefined method count rised by the model is correct? How about a method to assign a progressive number to FirePrevention.practice_number? -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 16 June 2011 21:50, Walter Lee Davis <waltd-HQgmohHLjDZWk0Htik3J/w@public.gmane.org> wrote: >> On Jun 16, 2011, at 5:46 PM, Mauro wrote: >> >>> On 16 June 2011 21:41, Walter Lee Davis <waltd-HQgmohHLjDZWk0Htik3J/w@public.gmane.org> wrote: >>>> >>>> On Jun 16, 2011, at 5:39 PM, Mauro wrote: >>>> >>>>> On 16 June 2011 21:36, Walter Lee Davis <waltd-HQgmohHLjDZWk0Htik3J/w@public.gmane.org> >>>>> wrote: >>>>>> >>>>>> On Jun 16, 2011, at 5:32 PM, Mauro wrote: >>>>>> >>>>>>> On 16 June 2011 21:24, Walter Lee Davis <waltd-HQgmohHLjDZWk0Htik3J/w@public.gmane.org> >>>>>>> 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. > > So the undefined method count rised by the model is correct? > How about a method to assign a progressive number to > FirePrevention.practice_number? > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > . > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en > . >Rob Biedenharn Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org http://AgileConsultingLLC.com/ rab-/VpnD74mH8+00s0LW7PaslaTQe2KTcn/@public.gmane.org http://GaslightSoftware.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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jun 16, 2011, at 5:53 PM, Mauro wrote:> On 16 June 2011 21:50, Walter Lee Davis <waltd-HQgmohHLjDZWk0Htik3J/w@public.gmane.org> wrote: >> On Jun 16, 2011, at 5:46 PM, Mauro wrote: >> >>> On 16 June 2011 21:41, Walter Lee Davis <waltd-HQgmohHLjDZWk0Htik3J/w@public.gmane.org> wrote: >>>> >>>> On Jun 16, 2011, at 5:39 PM, Mauro wrote: >>>> >>>>> On 16 June 2011 21:36, Walter Lee Davis <waltd-HQgmohHLjDZWk0Htik3J/w@public.gmane.org> >>>>> wrote: >>>>>> >>>>>> On Jun 16, 2011, at 5:32 PM, Mauro wrote: >>>>>> >>>>>>> On 16 June 2011 21:24, Walter Lee Davis <waltd-HQgmohHLjDZWk0Htik3J/w@public.gmane.org> >>>>>>> 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. > > 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 -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jun 16, 2011, at 3:12 PM, Rob Biedenharn 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... -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 Jun 16, 9:32 pm, Mauro <mrsan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 16 June 2011 21:24, Walter Lee Davis <wa...-HQgmohHLjDZWk0Htik3J/w@public.gmane.org> 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 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 16 June 2011 22:12, Rob Biedenharn <Rob-GBZH0y1GwQfnZcttdmLDtcI/UQi/AW5J@public.gmane.org> 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 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 On Jun 17, 1:07 am, Mauro <mrsan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 16 June 2011 22:12, Rob Biedenharn <R...-GBZH0y1GwQfnZcttdmLDtcI/UQi/AW5J@public.gmane.org> 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 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 17 June 2011 02:44, arai <thepro19-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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? -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.