在heroku上插入数据库错误但在本地工作,ActiveRecord :: StatementInvalid

时间:2012-02-06 03:13:33

标签: ruby-on-rails activerecord heroku

我有2个型号:

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
  ...

和迁移:

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

当我在heroku服务器上调用add_post时,出现错误:

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'

但有线的是,它可以在我的电脑上运行。我的操作系统信息:

ubuntu 10.04
ruby 1.9.3p0
rails 3.1.3
postgresql 8.4.10

感谢。

1 个答案:

答案 0 :(得分:1)

我解决了这个问题,但仍然不知道为什么。

在heroku控制台(heroku运行控制台)中尝试一些后,我发现错误不是由我的add_post方法引起的。实际上它是由我的MicroPost.new引起的:

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

正如此处显示的代码,MicroPost有一个datetime列,其名称为pub_date,此字段来自RSS项pubDate,此代码将导致下面的错误显示(在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'

经过一些谷歌搜索后,我找到了解决方案:http://www.spacebabies.nl/2008/06/16/rails-2-and-rss-parsing-gives-weird-errors/

虽然它是Rails2,但它仍适用于我。 只需将item.pubDate更改为item.pubDate.to_s

再次,有谁能解释这里发生的事情?

ps,item.pubDate没有错,在控制台我可以打印它,即:

item.pubDate.class => Time