当我尝试访问任何属性时,Rails 活动记录对象会导致错误
我有这样的代码:
@notifications = Notification.find_all_by_user_id(@user.id, :order=>'deliver_by DESC', :conditions=>"deliver_by >= '#{Date.today.to_s(:db)}'")
logger.info @upcoming_reminders[0].inspect
logger.info @upcoming_reminders[0].class
logger.info @upcoming_reminders[0].id
logger.info "--------------------------"
记录的结果是
#<TwitterNotification id: 1, user_id: 3, message: "Testing", deliver_by: "2009-10-30 16:00:00", created_at: "2009-10-13 12:00:45", updated_at: "2009-10-28 20:01:03", processed_at: "2009-10-13 20:00:06", type: "TwitterNotification">
TwitterNotification
ArgumentError (wrong number of arguments (1 for 0)):
app/controllers/reminders_controller.rb:57:in `get_reminders'
如您所见,在尝试访问 id 时出现参数错误。当我尝试访问任何属性时实际上会发生这种情况。这可能是盯着电脑太久的情况,但我一直没能弄清楚。有什么想法吗?请注意,TwitterNotification 是使用单表继承实现的通知的子类。
谢谢!
更新
我取得了一些进展,这工作正常:
logger.info @upcoming_reminders[0][:id]
因此使用符号访问活动记录对象属性工作正常,但点表示法不行。知道为什么吗?
I have code like:
@notifications = Notification.find_all_by_user_id(@user.id, :order=>'deliver_by DESC', :conditions=>"deliver_by >= '#{Date.today.to_s(:db)}'")
logger.info @upcoming_reminders[0].inspect
logger.info @upcoming_reminders[0].class
logger.info @upcoming_reminders[0].id
logger.info "--------------------------"
The logged result is
#<TwitterNotification id: 1, user_id: 3, message: "Testing", deliver_by: "2009-10-30 16:00:00", created_at: "2009-10-13 12:00:45", updated_at: "2009-10-28 20:01:03", processed_at: "2009-10-13 20:00:06", type: "TwitterNotification">
TwitterNotification
ArgumentError (wrong number of arguments (1 for 0)):
app/controllers/reminders_controller.rb:57:in `get_reminders'
As you can see, I get an argument error when trying to access the id. This actually occurs when I try to access any attribute. This may be a case of staring at the computer too long but I haven't been able to figure it out. Any ideas? Note that TwitterNotification is a subclass of Notification implemented using Single Table Inheritance.
Thanks!
UPDATE
I made a little progress, this works fine:
logger.info @upcoming_reminders[0][:id]
So accessing active record object attributes with a symbol works fine but dot notation does not. Any idea why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,不是盯着电脑太久的情况。我在我的模型中创建了一个名为 send 的方法,该方法似乎是不允许的。
Ok not a case of staring at my computer too long. I created a method called send in my model which does not seem to be allowed.