From: "Ruben D. Orduz" Date: 2006-10-26T22:37:12+09:00 Subject: Re: auto_increment model method wont increment but no error ? ------=_Part_41977_8774863.1161869830658 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline 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 On 10/25/06, Tim Pease wrote: > > On 10/25/06, Dan W. wrote: > > 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? > > > > 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 > > ------=_Part_41977_8774863.1161869830658--