未定义的方法“total_entries” Rails 2.2.2 升级到 2.3.5 后
我正在将 Rails 应用程序从 2.2.2 升级到 2.3.5。唯一剩下的错误是当我调用total_entries来创建jqgrid时。
错误:
NoMethodError (undefined method `total_entries' for #<Array:0xbbe9ab0>)
代码片段:
@route = Route.find(
:all,
:conditions => "id in (#{params[:id]})"
) {
if params[:page].present? then
paginate :page => params[:page], :per_page => params[:rows]
order_by "#{params[:sidx]} #{params[:sord]}"
end
}
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @route }
format.json { render :json => @route }
format.jgrid {
render :json => @route.to_jqgrid_json(
[
:id, :name
],
params[:page],
params[:rows],
@route.total_entries
)
}
end
有什么想法吗?谢谢!
编辑
我可以通过删除find
之后使用的块来使其工作。我还必须移动 squirrel 插件使用的 order_by
,因为我收到了未定义的方法调用。
我不喜欢这样的事实:由于必须在多个位置使用条件
,因此它比之前的代码不那么干。有没有更好的方法可以使用 Rails 2.3.5、will_paginate 和 squirrel 来做到这一点?
if params[:page].present? then
@route = Route.paginate :conditions => "id in (#{params[:id]})", :page => params[:page], :per_page => params[:rows], :order => "`#{params[:sidx]}` #{params[:sord]}"
else
@route = Route.find(:all, :conditions => "id in (#{params[:id]})")
end
编辑2
此错误的另一种可能性可能是我使用的是 Ruby 1.8.7 和 Rails 2.2.2,现在使用的是 Ruby 1.9.1 和 Rails 2.3.5。 1.8.7 和 1.9.1 之间是否有任何重大更改会阻止 ActiveRecord 发现后的块不运行?
I am upgrading a Rails application from 2.2.2 to 2.3.5. The only remaining error is when I invoke total_entries
for creating a jqgrid.
Error:
NoMethodError (undefined method `total_entries' for #<Array:0xbbe9ab0>)
Code snippet:
@route = Route.find(
:all,
:conditions => "id in (#{params[:id]})"
) {
if params[:page].present? then
paginate :page => params[:page], :per_page => params[:rows]
order_by "#{params[:sidx]} #{params[:sord]}"
end
}
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @route }
format.json { render :json => @route }
format.jgrid {
render :json => @route.to_jqgrid_json(
[
:id, :name
],
params[:page],
params[:rows],
@route.total_entries
)
}
end
Any ideas? Thanks!
EDIT
I am able to get it working by removing the block used after find
. I also had to move the order_by
used by the squirrel plugin as I was getting an undefined method call for it.
I don't like the fact that this is less DRY than the previous code by having to use conditions
in more than one location. Is there any better way to do this with Rails 2.3.5, will_paginate, and squirrel?
if params[:page].present? then
@route = Route.paginate :conditions => "id in (#{params[:id]})", :page => params[:page], :per_page => params[:rows], :order => "`#{params[:sidx]}` #{params[:sord]}"
else
@route = Route.find(:all, :conditions => "id in (#{params[:id]})")
end
EDIT 2
Another possibility for this error may be that I was using Ruby 1.8.7 with Rails 2.2.2 and am now using Ruby 1.9.1 with Rails 2.3.5. Were there any major changes between 1.8.7 and 1.9.1 that would prevent the block after the ActiveRecord find to not run?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 Rails 2.3.5 中,find(:all, ...) 调用将返回一个数组,通常这些数组没有任何与之关联的自定义方法,就像您可能在作用域中获得的那样。将块传递给 find 调用也有点不规则,可能是问题的一部分。
您可以通过创建一个找到您想要的内容的范围来解决此问题:
然后您可以改用该范围:
In Rails 2.3.5, a find(:all, ...) call will return an Array and generally these do not have any custom methods associated with them like you might get with a scope. Passing a block to a find call is also a little irregular and may be part of the problem.
You may be able to fix this by creating a scope that finds what you want:
Then you can use the scope instead: