How can I find efficiently the model Invoice with the greater code (invoice attribute) when the code is a string like "1/10", "2/10", "3/10" (without quotes) and where only the number before the slash count? 1/10 < 2/10 < 3/10 < 11/10 < 12/10, etc... -- Posted via http://www.ruby-forum.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.
Juan Kinunt
2010-Mar-29 12:48 UTC
Re: Find efficiently "greater" string in model attribute
Now I''m using: def after_initialize if self.new_record? and self.codigo.blank? max_codigo = FacturaEmitida.all.map{|f| f.codigo.split(''/'')[0].to_i}.max if !max_codigo.nil? self.codigo = (max_codigo + 1).to_s+''/''+Date.today.strftime("%y") else self.codigo = "1/"+Date.today.strftime("%y") end end end but I think it is very inefficient. -- Posted via http://www.ruby-forum.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 29 March 2010 13:38, Juan Kinunt <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> How can I find efficiently the model Invoice with the greater code > (invoice attribute) when the code is a string like "1/10", "2/10", > "3/10" (without quotes) and where only the number before the slash > count? 1/10 < 2/10 < 3/10 < 11/10 < 12/10, etc...If the 10 is the year then I would suggest storing the invoice number (1,2,3) separately from the year. Then build the full string (1/10) when you need it. You may not even need the year code if there is a created_at field. Colin -- 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.