# Increment feature is not working

**URL:** <https://www.ruby-forum.com/t/increment-feature-is-not-working/111661>\
**Category:** Rails\
**Created:** [August 9, 2007, 5:08pm UTC](https://www.ruby-forum.com/t/increment-feature-is-not-working/111661 "2007-08-09T17:08:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ajit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a3d4f5/32.png) [@Ajit](https://www.ruby-forum.com/u/Ajit)\
**Post date:** [August 9, 2007, 5:08pm UTC](https://www.ruby-forum.com/t/increment-feature-is-not-working/111661/1 "2007-08-09T17:08:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ajit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a3d4f5/32.png) [@Ajit](https://www.ruby-forum.com/u/Ajit)\
**Post date:** [August 9, 2007, 5:57pm UTC](https://www.ruby-forum.com/t/increment-feature-is-not-working/111661/2 "2007-08-09T17:57:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ajit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a3d4f5/32.png) [@Ajit](https://www.ruby-forum.com/u/Ajit)\
**Post date:** [August 9, 2007, 6:29pm UTC](https://www.ruby-forum.com/t/increment-feature-is-not-working/111661/3 "2007-08-09T18:29:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ajit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a3d4f5/32.png) [@Ajit](https://www.ruby-forum.com/u/Ajit)\
**Post date:** [August 9, 2007, 6:16pm UTC](https://www.ruby-forum.com/t/increment-feature-is-not-working/111661/4 "2007-08-09T18:16:26Z")

</div>

can’t convert Fixnum into Array… this is the error message i am  
getting

On Aug 9, 11:57 am, Emmanuel O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
