views:

242

answers:

1

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?

+1  A: 

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])
tadman
The find(:all) in Rails 2.2.2 also returns an Array. Is there any known incompatibility between 2.3.5 and will-paginate?
Trevor
I don't know of any specific issues if it's used in its regular form. Do you mean to use @route.length instead in this case?
tadman