当我尝试访问任何属性时,Rails 活动记录对象会导致错误

发布于 2024-08-09 17:10:41 字数 1089 浏览 8 评论 0原文

我有这样的代码:

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深巷少女 2024-08-16 17:10:41

好吧,不是盯着电脑太久的情况。我在我的模型中创建了一个名为 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文