kevin lynx
kevin lynx

Reputation: 895

insert to database error on heroku but worked locally, ActiveRecord::StatementInvalid

I have 2 models:

class User < ActiveRecord::Base
  has_many :micro_posts, :dependent => :destroy
  ..
  def add_post(post)
    self.micro_posts << post
  end

class MicroPost < ActiveRecord::Base
  belongs_to :user
  ...

and the migration:

class CreateMicroPosts < <ActiveRecord::Migration
  def change
    create_table :micro_posts do |t| 
    ...

And when I called add_post on heroku server, I got an error:

2012-02-06T02:59:58+00:00 app[web.1]: ActiveRecord::StatementInvalid (ArgumentError: wrong number of arguments (1 for 0): INSERT INTO "micro_posts" ("created_at", "description", "pub_date", "tag", "title", "updated_at", "url", "user_id") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id"):
2012-02-06T02:59:58+00:00 app[web.1]:   app/models/user.rb:25:in `add_post'

But the wired thing is, it worked on my computer. My OS info:

ubuntu 10.04
ruby 1.9.3p0
rails 3.1.3
postgresql 8.4.10

Thanks.

Upvotes: 0

Views: 450

Answers (1)

kevin lynx
kevin lynx

Reputation: 895

I solve this problem, but still do not know why.

After some trying in heroku console (heroku run console), I found the error is not caused by my add_post method. Actually it was caused by my MicroPost.new:

post = MicroPost.new(..., :pub_date => rss_item.pubDate)

As the code shows here, MicroPost has a datetime column which names pub_date, and this field come from a RSS item pubDate, and this code will lead to the error shows below (on heroku):

irb(main):020:0> post=MicroPost.new(:description=>"none", :url=>'url',:title=>"title",:pub_date=>item.pubDate,:tag=>"tag")
ArgumentError: wrong number of arguments (1 for 0)
from /usr/local/lib/ruby/1.9.1/time.rb:454:in `rfc2822'
from /app/vendor/bundle/ruby/1.9.1/gems/activesupport-3.1.3/lib/active_support/time_with_zone.rb:168:in `to_s'
from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.1.3/lib/active_record/base.rb:1785:in `attribute_for_inspect'

After some googling, i found the solution:http://www.spacebabies.nl/2008/06/16/rails-2-and-rss-parsing-gives-weird-errors/

Although it's Rails2, it still work for me. just change item.pubDate to item.pubDate.to_s

Again, can anyone explain what happened here?

ps, item.pubDate is not wrong, in console i can print it, i.e:

item.pubDate.class => Time

Upvotes: 1

Related Questions