# Auto\_increment model method wont increment but no error?

**URL:** <https://www.ruby-forum.com/t/auto-increment-model-method-wont-increment-but-no-error/73663>\
**Category:** Ruby\
**Created:** [October 26, 2006, 1:30am UTC](https://www.ruby-forum.com/t/auto-increment-model-method-wont-increment-but-no-error/73663 "2006-10-26T01:30:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [October 26, 2006, 1:30am UTC](https://www.ruby-forum.com/t/auto-increment-model-method-wont-increment-but-no-error/73663/1 "2006-10-26T01:30:58Z")

</div>

Hi, i’m trying to get my ChrissMonster model to pull back the value of  
“eat” in the chriss\_monster table and increment the value by 1.

Here’s my controller function:

> > >

def feed  
@chriss\_monster = ChrissMonster.find(params[:id])  
@chriss\_monster.feed  
redirect\_to :action =\> ‘list’  
end  
\<\<\<

Here’s my model:

> > >

class ChrissMonster \< ActiveRecord::Base  
def feed  
ChrissMonster.increment\_counter :eat, :id  
end  
end  
\<\<\<

When I call the controller function, it directs me back to the list  
page, it doesn’t give me an error, and it does NOT increment the “eat”  
value.

What am I doing wrong? How else could I rewrite this?

Thanks for reading!

–Dan W.

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [October 26, 2006, 5:53am UTC](https://www.ruby-forum.com/t/auto-increment-model-method-wont-increment-but-no-error/73663/2 "2006-10-26T05:53:24Z")

</div>

On 10/25/06, Dan W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> \<\<\<  
> When I call the controller function, it directs me back to the list  
> page, it doesn’t give me an error, and it does NOT increment the “eat”  
> value.
> 
> What am I doing wrong? How else could I rewrite this?

The problem is in your model. My assumption is that your database  
table for ChrissMonster has an ‘eat’ column and an ‘id’ column with  
‘id’ being the primary key. It looks like your trying increment the  
‘eat’ counter.

class ChirssMonster \< ActiveRecord::Base  
def feed  
ChrisMonster.increment\_counter ‘eat’, self[‘id’]  
end  
end

This might work. The difference is that I’m telling active record to  
increment the ‘eat’ couonter for the record that has the primary key  
of self[‘id’] – the id of the record on which the feed method was  
invoked.

A shorter version that might work …

class ChrissMonster \< ActiveRecord::Base  
def feed  
increment!( ‘eat’ )  
end  
end

That’s all I got. Try posting this question on the ruby on rails list.  
They are a lot smarter about rails over there.

Blessings,  
TwP

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [October 26, 2006, 3:37pm UTC](https://www.ruby-forum.com/t/auto-increment-model-method-wont-increment-but-no-error/73663/3 "2006-10-26T15:37:58Z")

</div>

Dan,

Your schema/model need work. What you need to do is retrieve the value  
of  
eat that corresponds with the param[:id] increment it (within your code  
[eat  
+= 1;]), and then store it back to the DB. Else what might be going on  
is  
that you’re telling the DB to create a new record with the counter and  
eat  
incremented. There’s no point in having the field eat to auto\_increment  
if  
it’s not going to be a key of the relation. That is, eat will not  
uniquely  
identify the row, thus you should not have it as an implicit key. If you  
don’t want to write one more line of code in your code…you can write 6  
lines and create a stored procedure in the DB (if your DB supports it)  
😉

Peace,  
Ruben

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [October 26, 2006, 6:06pm UTC](https://www.ruby-forum.com/t/auto-increment-model-method-wont-increment-but-no-error/73663/4 "2006-10-26T18:06:17Z")

</div>

Dan W. wrote:

> \<\<\<

Wouldn’t you need a @chriss\_monster.save in there somewhere?

-Doug Seifert

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [October 26, 2006, 5:46pm UTC](https://www.ruby-forum.com/t/auto-increment-model-method-wont-increment-but-no-error/73663/5 "2006-10-26T17:46:15Z")

</div>

On 10/26/06, Ruben D. Orduz [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> lines and create a stored procedure in the DB (if your DB supports it) 😉

Dan, take a look at the documentaiton on the ActiveRecord “increment!”  
method. I think this does what you just described.

Blessings,  
TwP

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [October 26, 2006, 10:02pm UTC](https://www.ruby-forum.com/t/auto-increment-model-method-wont-increment-but-no-error/73663/6 "2006-10-26T22:02:45Z")

</div>

On 10/26/06, Rick DeNatale [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> So it SHOULD act like
> 
> self.increment\_counter(:eat)
> 
> Not sure why it doesn’t seem to be.

OOPS, that should have been

So it SHOULD act like

```
  self.increment_counter!(:eat)

```

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

IPMS/USA Region 12 Coordinator  
[http://ipmsr12.denhaven2.com/](http://ipmsr12.denhaven2.com/)

Visit the Project Mercury Wiki Site  
[http://www.mercuryspacecraft.com/](http://www.mercuryspacecraft.com/)

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [October 26, 2006, 10:02pm UTC](https://www.ruby-forum.com/t/auto-increment-model-method-wont-increment-but-no-error/73663/7 "2006-10-26T22:02:45Z")

</div>

On 10/26/06, Ruben D. Orduz [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Dan,
> 
> Your schema/model need work. What you need to do is retrieve the value of  
> eat that corresponds with the param[:id] increment it (within your code [eat  
> += 1;]), and then store it back to the DB.

I thought something along these lines also, but looking at the  
documentation for increment\_all  
[http://rubyonrails.org/api/classes/ActiveRecord/Base.html#M000871](http://rubyonrails.org/api/classes/ActiveRecord/Base.html#M000871)

and following the source code looks like it will run a SQL query  
something like

```
UPDATE ChrissMonsters SET eat = eat + 1 WHERE id = self_id;

```

where self\_id is the id of the particular instance. Notice that  
increment\_all is a class method of ActiveRecord::Base

So it SHOULD act like

self.increment\_counter(:eat)

## Not sure why it doesn’t seem to be.

Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
