问题描述
我有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时,出现了一个错误:
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
谢谢。
推荐答案
我解决了这个问题,但仍然不知道为什么。
I solve this problem, but still do not know why.
在heroku控制台(heroku运行控制台)中进行了一些尝试之后,我发现错误不是由我的add_post方法引起的。实际上是由我的MicroPost.new引起的:
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)
如代码所示,MicroPost的datetime列名为pub_date,该字段来自RSS项目pubDate,此代码将导致以下错误显示(在heroku上):
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'
经过一番谷歌搜索后,我找到了解决方法:http://www.spacebabies.nl/2008/06/16/rails-2-and-rss-parsing-gives-weird-errors/
After some googling, i found the solution:http://www.spacebabies.nl/2008/06/16/rails-2-and-rss-parsing-gives-weird-errors/
尽管它是Rails2,但它仍然对我有用。 只需将item.pubDate更改为item.pubDate.to_s
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没错,我可以在控制台中打印它,即:
ps, item.pubDate is not wrong, in console i can print it, i.e:
item.pubDate.class => Time
这篇关于在heroku上插入数据库错误但在本地工作,ActiveRecord :: StatementInvalid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!