【问题标题】:undefined method `call' for #<ActiveRecord::Relation []>#<ActiveRecord::Relation []> 的未定义方法“调用”
【发布时间】:2014-10-17 05:14:39
【问题描述】:
 def index
    @posts = Post.published

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @posts }
      format.atom
    end
  end

我遇到了这个错误。而且我是 RoR 的新手。谁能帮助我。我现在能做什么?

【问题讨论】:

  • 你能显示错误的回溯吗?
  • app/controllers/posts_controller.rb:3:in `index'
  • 发布的是方法还是关系?

标签: ruby-on-rails ruby activerecord


【解决方案1】:

您已经定义了一个范围,但给了它一个关系而不是一个过程。你可能有这样的事情:

class Post < ActiveRecord::Base
  scope :published, where(published: true)
end

改成这样:

class Post < ActiveRecord::Base
  scope :published, -> { where(published: true) }
end

以后,请始终发布整个堆栈跟踪以及所涉及的方法。猜测发生了什么并不总是那么容易。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-06
    • 2021-09-24
    • 2013-12-16
    • 1970-01-01
    • 1970-01-01
    • 2015-08-09
    相关资源
    最近更新 更多