Find efficiently "greater" string in model attribute

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…

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.

On 29 March 2010 13:38, Juan K. [email protected] 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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs