mislav / will_paginate

Pagination library for Rails and other Ruby applications
http://github.com/mislav/will_paginate/wikis
MIT License
5.7k stars 864 forks source link

will_paginate error in production NoMethodError (undefined method `page' for []:ActiveRecord::Relation): #308

Closed yasinishyn closed 11 years ago

yasinishyn commented 11 years ago

I'm using will_paginate gem for ajax-like "see-more" link. It works in development but fails in production.

In my production log I get:

NoMethodError (undefined method `page' for []:ActiveRecord::Relation):

I've tried, in root of production app to type:

grep will_paginate Gemfile.lock

and get:

will_paginate (3.0.4)
will_paginate

cap bundle:install don't fails installing will_paginate on my VPS, but when I tried to explicitly require 'will_paginate' it steel works in development and fails in production, but this time the whole site, not only action in which I've used will_paginate. This requiring triggered an error in unicorn.log:

No such file to load will_paginate (LoadError)

So I think that will_paginate don't instals his directories on my VPS properly wih bundle install, but why?

I tried to type:

gem list

and there where no will_paginate. So I explicitly, in my current app dir. gem install will_paginate and that also didn't resolve the issue.

So I gem uninstall will_paginate and "cap deploy" again after I again gem list and again there where no will_paginate Gemfile:

gem 'rails', '3.2.13'

gem 'pg'

gem 'will_paginate'
gem 'sass-rails'
gem 'jquery-rails'
gem 'jquery-ui-rails'
gem 'devise'
gem 'uglifier'
gem 'coffee-rails'
gem "haml"
gem 'twitter-bootstrap-rails'
gem 'bootstrap-addons-rails'
gem 'bootstrap-wysihtml5-rails'
gem 'therubyracer'
gem 'less-rails'

#deploy
gem 'unicorn'
gem 'capistrano'

gem "rmagick"
gem "carrierwave"
gem 'jquery-fileupload-rails'

group :development do
gem 'better_errors'
gem 'binding_of_caller'
gem 'meta_request'
end

group :development, :test do
    gem 'rspec-rails'
end
group :test do
    gem 'factory_girl_rails'
    gem 'capybara'
    gem 'guard-rspec'
    gem 'growl'
    gem 'guard-spork'
    gem 'spork'
    gem 'rb-fsevent', '~> 0.9'
    gem "faker"
 end

deploy.rb

require "bundler/capistrano" 

server ".....", :web, :app, :db, primary: true

set :application, "......" 
set :user, "......." 
set :deploy_to, "/home/#{user}/apps/#{application}" 
set :deploy_via, :remote_cache 
set :use_sudo, false
set :shared_children, shared_children + %w{public/uploads}

set :scm, "git" 
set :repository, "git@github.com:........git" 
set :branch, "master"

default_run_options[:pty] = true 
ssh_options[:forward_agent] = true

after "deploy", "deploy:cleanup" # keep only the last 5 releases

namespace :deploy do 
  namespace :assets do
    task :precompile, :roles => :web do
      from = source.next_revision(current_revision)
      if capture("cd #{latest_release} && #{source.local.log(from)} vendor/assets/ lib/assets/ app/assets/ | wc -l").to_i > 0
        run_locally("rake assets:clean && rake assets:precompile")
        run_locally "cd public && tar -jcf assets.tar.bz2 assets"
        top.upload "public/assets.tar.bz2", "#{shared_path}", :via => :scp
        run "cd #{shared_path} && tar -jxf assets.tar.bz2 && rm assets.tar.bz2"
        run_locally "rm public/assets.tar.bz2"
        run_locally("rake assets:clean")
      else
        logger.info "Skipping asset precompilation because there were no asset changes"
      end
    end

    task :symlink, roles: :web do
      run ("rm -rf #{latest_release}/public/assets &&
            mkdir -p #{latest_release}/public &&
            mkdir -p #{shared_path}/assets &&
            ln -s #{shared_path}/assets #{latest_release}/public/assets")
    end
  end

%w[start stop restart].each do |command|
    desc "#{command} unicorn server" 
    task command, roles: :app, except: {no_release: true} do
        run "/etc/init.d/unicorn_#{application} #{command}" 
    end
end

task :setup_config, roles: :app do 
    sudo "ln -nfs #{current_path}/config/nginx.conf /etc/nginx/sites-enabled/#{application}" 
    sudo "ln -nfs #{current_path}/config/unicorn_init.sh /etc/init.d/unicorn_#{application}" 
    run "mkdir -p #{shared_path}/config" 
    put File.read("config/database.example.yml"), "#{shared_path}/config/database.yml" 
    puts "Now edit the config files in #{shared_path}."
end
after "deploy:setup", "deploy:setup_config"

task :symlink_config, roles: :app do 
    run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
end
after "deploy:finalize_update", "deploy:symlink_config"
desc "Make sure local git is in sync with remote." 
task :check_revision, roles: :web do
    unless `git rev-parse HEAD` == `git rev-parse origin/master` 
        puts "WARNING: HEAD is not the same as origin/master"
        puts "Run `git push` to sync changes." 
        exit
    end 
end 
before "deploy", "deploy:check_revision" 
#rake seed task
desc "Seed the database on already deployed code"
task :seed, :only => {:primary => true}, :except => { :no_release => true } do
    run "cd #{current_path}; RAILS_ENV=#{rails_env} bundle exec rake db:seed"
end
desc "Seed the database on already deployed code"
task :drop, :only => {:primary => true}, :except => { :no_release => true } do
    run "cd #{current_path}; RAILS_ENV=#{rails_env} bundle exec rake db:drop:all"
    run "cd #{current_path}; RAILS_ENV=#{rails_env} bundle exec rake db:create:all"
    run "cd #{current_path}; RAILS_ENV=#{rails_env} bundle exec rake db:migrate"
end

end

Can anyone help me with this issue?

Or maybe Someone knows how to explicitly add all files, needed for will_paginate, to rails app.? So it will work without gem will_paginate

I tied to:

bundle exec gem list

and in the list a find will_paginate (3.0.4). It means that it is installed but not for my app?

mislav commented 11 years ago

Your Gemfile and Gemfile.lock look OK. The information above is indicative of a problem with Ruby or Bundler in production. I can't tell exactly what's going on, but it seems that Unicorn runs in a Ruby environment different than the one you ran bundle install with.

It shouldn't be hard to debug this. From your applications directory on the server, run:

gem env
bundle show will_paginate
script/rails runner -e production 'WillPaginate'

The first command shows you information about Ruby version, RubyGems version, installation paths for gems etc. The next command shows you where will_paginate got installed physically by Bundler. The last boots up Rails and confirms that will_paginate has loaded (it will error out if not).

The next step is to ensure Unicorn runs in that same environment (same Ruby version, most importantly). Start from the /etc/init.d/unicorn_YOUR_APP script and see how things are set up.

I won't help you further setting up your server because that's your job. Good luck!

yasinishyn commented 11 years ago

Thank you alot, for your answer. You are right Mislav , on my VPS I'm using ruby 2.0.0 and in local environment 1.9.3. But after all, why only will_paginate ?? all other gems are installing and working properly. So I change local ruby to 2.0.0-p0 as on server, run script/rails runner -e production 'WillPaginate' (with no error), and still a have this error. You were right, it is "hard to debug this".

yasinishyn commented 11 years ago

Finally the long sleepless night is over. The recipe is: gem update --system (on both: server and local machine) update local ruby version, to ruby version as on server, or vice versa cap deploy:setup (don't now if it is necessary, but it works for me) cap deploy && cap deploy:restart and BAMM, it works.