views:

50

answers:

2

The following line works fine in MySQL, but in Postgresql I get a Rails error stating, "can't convert Fixnum to String".

viewable.update_attribute(:total_views, viewable.total_views.to_i + 1)

total_views is an integer field

Shouldn't Rails know to treat total_views as an integer even if I am using Postgresql?

A: 

I'm not sure if you would have the same problem with the

update_attributes

method. Since the

update_attribute

method skips Rails validations.Try using the former, and you may get a better picture on what's going on behind the scenes.

Shreyas Satish
Not sure how validations would make a difference, but maybe they would invoke a show fields. I'll give it a try.
Jared Brown
A: 

The short answer is "yes" Rails should know how to deserialize that column as an integer. What version of Rails and what Postgres adapter are you using?

As a bonus, though, you can do the following to accomplish what you want:

viewable.increment(:total_views)
semanticart
p = self.total_likes.to_i + self.total_comments.to_i * 2 Notice the .to_i methods I have to call. I am using Rails 2.3.4 on Heroku.
Jared Brown
postgres (0.7.9.2008.01.28) is the gem installed in Heroku
Jared Brown
When I removed the .to_i method calls and tested on MySQL it worked fine. But under Postgresql I get the error.
Jared Brown
The fields are set to NOT NULL and DEFAULT 0
Jared Brown
I'm surprised no one else has run into this.
Jared Brown