3

我正在将 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。我还必须移动order_bysquirrel 插件使用的 squirrel 插件,因为我得到了一个未定义的方法调用。

我不喜欢这样一个事实,即由于必须conditions在多个位置使用它,所以它比以前的代码 DRY 少。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 发现后的块无法运行?

4

1 回答 1

1

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:

class Route < ActiveRecord::Base
  named_scope :with_ids, lambda { |*ids| {
    :conditions => { :id => ids.flatten }
  }}
end

Then you can use the scope instead:

@routes = Route.with_ids(params[:id])
于 2010-05-17T15:56:33.857 回答