Hello i have a custom query which is getting max number from one of column. I want to get that number increment by 1 and then save that in different table. please refer following code: def getmaxphasenumber(templaid) self.find_by_sql("select max(templateid) from templates") end maxnumber = Templatephases.getmaxphasenumber(templateid) if !maxnumber.blank? tmpnumber = maxnumber + 1 else tmpnumber = 1 end tmpnumber is returning an error. i tried maxnumber.to_i didnt work. Please advice thanks Ajit --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
Try with ActiveRecord computations: def getmaxphasenumber(templaid) Templatephases.transaction do current_max= Templatephases.maximum :templateid #, #:conditions => #["field = ?", required_field] max= current_max ? current_max + 1 : 1 # Do whatever you want with the max here. end end I wrap the maximum seach in a transaction to ensure the maximum KEEPS BEING maximum till i end what i what to do with that maximum.... do you understand? (if someone saves a new record with a new maximum while i''m working in the db my maximum could no longer be valid).> def getmaxphasenumber(templaid) > self.find_by_sql("select max(templateid) from templates") > end > > maxnumber = Templatephases.getmaxphasenumber(templateid) > > if !maxnumber.blank? > tmpnumber = maxnumber + 1 > else > tmpnumber = 1 > end-- 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
can''t convert Fixnum into Array.. this is the error message i am getting On Aug 9, 11:57 am, Emmanuel Oga <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Try with ActiveRecord computations: > > def getmaxphasenumber(templaid) > > Templatephases.transaction do > > current_max= Templatephases.maximum :templateid #, > #:conditions => > #["field = ?", > required_field] > > max= current_max ? current_max + 1 : 1 > > # Do whatever you want with the max here. > end > > end > > I wrap the maximum seach in a transaction to ensure the maximum KEEPS > BEING maximum till i end what i what to do with that maximum.... do you > understand? (if someone saves a new record with a new maximum while i''m > working in the db my maximum could no longer be valid). > > > def getmaxphasenumber(templaid) > > self.find_by_sql("select max(templateid) from templates") > > end > > > maxnumber = Templatephases.getmaxphasenumber(templateid) > > > if !maxnumber.blank? > > tmpnumber = maxnumber + 1 > > else > > tmpnumber = 1 > > end > > -- > Posted viahttp://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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
Thanks my friend.... i was doing it wrong way.... dropped the idea of custom query... used Templatephase.maximum and it worked thanks a ton Ajit --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---