SQLite3: no such column: "id desc" - ruby-on-rails

I'm working on a piece of very old code (ruby 1.9.3 and rails 2.3.12). I'm trying to write some tests for it so that when I start upgrading it I have an idea of how everything is working. I'm writing them in cucumber at the moment and have come across an issue that I can't seem to solve. This issue does not appear in the production version as everything works fine on that. In the development AND test however, when I try and click one of the links, the following error appears;
And I click "Trips (1)" # features/step_definitions/user_management_steps.rb:1
SQLite3::SQLException: no such column: "id desc": SELECT "trips".* FROM "trips" INNER JOIN "trips_users" ON "trips".id = "trips_users".trip_id WHERE (("trips_users".user_id = 1)) ORDER BY ["id desc"] LIMIT 10 OFFSET 0 (ActiveRecord::StatementInvalid)
./vendor/plugins/geokit-rails/lib/geokit-rails/acts_as_mappable.rb:157:in `find'
./app/controllers/users_controller.rb:259:in `trips'
/home/camillavk/.rvm/rubies/ruby-1.9.3-p551/lib/ruby/1.9.1/benchmark.rb:295:in `realtime'
<internal:prelude>:10:in `synchronize'
./features/step_definitions/user_management_steps.rb:2:in `/^I click "([^"]*)"$/'
features/trips.feature:30:in `And I click "Trips (1)"'
I've looked everywhere and cannot find any reference to
no such column: "id desc"
does anyone know what this means?
And why does this break in the development and test environments but not in the production environment?
Any help would be greatly appreciated!
*Edit
gemfile;
source 'https://rubygems.org'
gem "rails", "~> 2.3.12"
gem "airbrake"
gem "aws-s3"
gem "bj"
gem "calendar_date_select", "1.15"
gem "cancan", "1.2.0"
gem "carmen"
gem 'comma', '0.4.1'
gem 'exifr'
gem "factory_girl", "1.2.4"
gem 'fastercsv'
gem "geokit"
gem "haml"
gem "hpricot"
gem 'hoptoad_notifier'
gem "jrails"
gem "libxml-ruby", "2.4.0"
gem "memcached", "~> 1.8.0"
gem "mysql2", "~> 0.2.6"
gem "rake", "0.8.3"
gem 'recurly'
gem "rdoc"
gem "rmagick"
gem "rspreedly"
gem "rubyzip"
gem 'sass'
#need to use simplecov when upgrading to ruby 2 as rcov doesn't work
gem "simplecov"
gem 'sqlite3'
gem "will_paginate", "2.3.16"
gem 'yaml_db'
group :development, :test do
gem 'test-unit', '1.2.3'
gem 'rspec', '1.3.2'
gem 'rspec-rails', '~> 1.3.4'
end
group :test do
gem 'capybara', '0.3.5'
gem 'cucumber', '1.1.2'
gem 'cucumber-rails', :git => "git://github.com/RKelln/cucumber-rails.git", :branch => "0.3.2-capybara-fix"
gem 'database_cleaner'
end
user_controller.rb;
class UsersController < ApplicationController
before_filter :find_user
load_and_authorize_resource :except => [:news, :search, :update, :mobile_new, :mobile_success]
before_filter :authorise, :only => [:export_to_csv]
def find_user
begin
#user = User.find_by_login(params[:id])
rescue
#user = User.find(params[:id])
end
end
....
....
def trips
body_attributes "view-followers", 'main-feed', 'Trips'
#page = (params[:page].to_i || 0)
#page = 1 if #page == 0
#trips = #user.trips.reverse_order
#year = ''
#month = ''
unless params[:year].blank?
#year = params[:year].to_i
#trips = #trips.select { |trip| trip.start_date.year == #year }
end
unless params[:month].blank?
#month = params[:month].to_i
#trips = #trips.select { |trip| trip.start_date.month == #month }
end
#years = Array.new
unless #user.trips.empty?
#years = #user.trips.first.start_date.year..#user.trips.last.start_date.year
end
#show_more_link = #trips.count > (10 * #page)
#trips = #trips.paginate(:per_page => 10, :page => #page)
end

It looks like you have an order on your Trips query defined like this Trip.order('[id desc]'). The correct format is (depending on your Rails version):
Trip.order('id DESC')
Trip.order(id: :desc) # works only in Rails >= 4.0

Related

Rails server says it cannot load the bcrypt gem?

When I try to do this action I get the "LoadError (cannot load such file -- bcrypt)"
require 'bcrypt'
class PublicController < ApplicationController
def attempt_login
if params[:email].present? && params[:password].present?
found_user = User.where(:email => params[:email]).first
if found_user
authorized_user = found_user.authenticate(params[:password])
end
end
if authorized_user
render(:text =>'authorized')
redirect_to(:controller => 'private', :action => 'home')
else
flash[:notice] = "Invalid email or password"
render :new
end
end
end
User model:
class User < ApplicationRecord
has_secure_password
end
I have tried all of the following in the gemfile and I get the same error every time:
gem 'bcrypt', '~>3.1.11'
gem 'bcrypt', '~> 3.1', '>= 3.1.11'
gem 'bcrypt', platforms: :ruby
How can I get it to load the bcrypt gem?
I'm using Windows 10.
Try this:
Remove bcrypt from your Gemfile.
Uninstall from your system by running gem uninstall bcrypt.
Add gem 'bcrypt', git: 'https://github.com/codahale/bcrypt-ruby.git', :require => 'bcrypt' to your Gemfile.
Run bundle install.
If you get errors, install Git https://git-scm.com/downloads and retry bundle install.
Source: https://github.com/codahale/bcrypt-ruby/issues/102#issuecomment-284118731

While using rspec i am constantly getting the same error

I am getting the same Error on running rspec that is :-
"Undefined Method - authorize_manage_partner_links"
I am using CANCAN gem in my Gemfile for authorization purposes and i am using Rails version 3.2 and Ruby version 1.9.2.
Please suggest what to do as i am totally new to rspec and rails. I am using a testing database, on importing the database for the first time, when i am running rspec it is showing successful but after that it is showing this error on running the same rspec file. I am using FactoryGirl. and i have defined everything in my factoryrb file as well.
This is my controller--
require 'will_paginate/array'
class PartnerLinksController < ApplicationController
#load_and_authorize_resource
before_filter :authorize_manage_partner_links!
layout "backoffice"
def index
#partner_links = PartnerLink.all
if params[:sortby]
#partner_links = #partner_links.sort_by {|p| p.name} if params[:sortby] == "up"
#partner_links = #partner_links.sort_by {|p| p.name}.reverse if params[:sortby] == "down"
else
#partner_links = #partner_links.sort_by {|p| p.name}
end
total = #partner_links.size
#partner_links = #partner_links.paginate(:page => params[:page], :per_page => 20)
page = params[:page]
if !page
page = 1
end
render :locals => {:total => total, :page => page, :sortby => params[:sortby], :partner_link => #partner_link}
end
def new
#partner_link = PartnerLink.new
end
def edit
#partner_link = PartnerLink.find(params[:id])
end
def create
#partner_link = PartnerLink.new(params[:partner_link])
if #partner_link.save
redirect_to partner_links_url
else
render "new"
end
end
def update
#partner_link = PartnerLink.find(params[:id])
if #partner_link.update_attributes(params[:partner_link])
redirect_to partner_links_url
else
render "edit"
end
end
def destroy
#partner_link = PartnerLink.find(params[:id])
#partner_link.destroy
redirect_to partner_links_url
end
end
This is my rspec controller that i am using--
require 'spec_helper'
describe PartnerLinksController do
before do
FactoryGirl.create(:partner_link)
role_permission
admin_role
user
currency
category
merchant
cart
deal
sign_in user
end
let(:role_permission) {FactoryGirl.create(:role_permission)}
let(:currency) {FactoryGirl.create(:currency)}
let(:merchant) {FactoryGirl.create(:merchant)}
let(:cart) {FactoryGirl.create(:cart)}
let(:deal) {FactoryGirl.create(:deal)}
let(:category) {FactoryGirl.create(:category)}
let(:authentication) {FactoryGirl.create(:authentication)}
let(:user) {FactoryGirl.create(:user)}
let(:admin_role) {FactoryGirl.create(:admin_role)}
describe "index" do
it "Should display all the partner links" do
get :index
end
end
describe "new" do
it "Should create all the partner links" do
get :new
end
end
describe "create" do
it "Should create new all the partner links" do
get :create
end
end
describe "update" do
it "Should update all the partner links" do
get :update
end
end
describe "destroy" do
it "Should delete all the partner links" do
get :destroy
end
end
end
app/models/partner_link.rb--
class PartnerLink < ActiveRecord::Base
# attr_accessible :title, :body
validates_presence_of :name, :url, :description
end
This is my Gemfile--
source 'http://rubygems.org'
ruby '1.9.2'
gem 'rails', '3.2.11'
gem 'authlogic'
gem "devise"
gem "devise-encryptable"
gem "devise-async"
gem 'omniauth'
gem 'omniauth-facebook'
gem 'omniauth-twitter'
gem "cancan"
gem 'activemerchant'
gem 'aws-sdk'
gem 'aws-s3'
gem 'paperclip', '~> 3.0'
gem 'oauth2'
#gem 'fastercsv' #no-longer required after ruby 1.9
gem 'pg'
#gem 'thin'
gem 'unicorn'
gem 'simple_form'
gem 'haml-rails' # Use HAML instead of ERB for templates
gem 'friendly_id'
gem 'will_paginate'
gem 'will_paginate-bootstrap'
gem "nifty-generators", :group => :development
#gem "pdfkit"
gem "pdfkit", "~> 0.5.0"
gem 'wkhtmltopdf-binary'
gem 'wicked_pdf'
gem 'sendgrid', "~> 1.0.1"
gem 'delayed_job'
gem 'delayed_job_active_record'
gem 'bootstrap-datepicker-rails'
#gem 'roadie' #fixing email css
group :development, :test do
gem 'rspec-rails'
gem 'guard-rspec'
# Database Cleaner clears the database between tests. This done because we have
# to set config.use_transactional_fixtures = false in the spec_helper file.
# The use_transactional_fixtures value is set to false because Selenium test
# driver cannot access the test records created via database transactions.
gem 'database_cleaner'
end
group :test do
# gem 'factory_girl_rails'
gem 'capybara'
# As we’re using Capybara we can call the save_and_open_page method at any
# point and this will open the page in a browser so that we can take a look
# at it. This is enabled by Launchy.
# gem 'launchy'
# gem 'rspec-mocks'
end
gem "less-rails"
gem 'twitter-bootstrap-rails', '>= 2.0.3'
gem 'rb-readline', '~> 0.5.0', require: 'readline'
gem 'execjs'
gem 'therubyracer'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', " ~> 3.2.3"
gem 'coffee-rails', "~> 3.2.1"
gem 'uglifier', '>= 1.0.3'
end
gem "mail", "~> 2.4.4"
gem 'jquery-rails'
#gem "mocha", :group => :test
gem 'gmaps4rails'
gem 'acts_as_xlsx'
gem "httparty"
gem 'newrelic_rpm'
gem 'font_assets'
gem 'paper_trail', '~> 3.0.6'
gem 'mailchimp-api', require: 'mailchimp'
gem 'rack-ssl-enforcer'
gem 'ckeditor'
gem "rails_best_practices"

ActiveAdmin - can't create model "Rating"

I have no idea why I get the error when I click on "New Rating". And it seems creation of other models gives the same error.
Here's the error msg: "no member 'rating' in struct"
The error is on line 316 of inherited_resources-1.4.0/lib/inherited_resources/base_helpers.rb
Full stacktrace: https://gist.github.com/depy/5330548
313 # extract attributes from params
314 def build_resource_params
315 parameters = respond_to?(:permitted_params) ? permitted_params : params
316 rparams = [parameters[resource_request_name] || parameters[resource_instance_name] || {}]
317 if without_protection_given?
318 rparams << without_protection
319 else
320 rparams << as_role if role_given?
321 end
Here's my Rating model:
class Rating < ActiveRecord::Base
validates_uniqueness_of :task_id, :scope => [:user_id, :user_role], :message => 'You have already rated this task'
validates_inclusion_of :user_role, :in => %w( client contractor )
validates_presence_of :task_id, :user_id
validates_presence_of :score, :message => 'Please rate your satisfaction'
belongs_to :task
belongs_to :user
end
Here's my ActiveAdmin Rating.rb:
ActiveAdmin.register Rating do
controller do; include StrongAdmin; end
# list
index do
column :user_id
column :task_id
column :comment
column :score
default_actions
end
# single
show do |rating|
attributes_table do
row :id
row :rater do
task_id = Rating.find(params[:id]).task_id
div do
Task.find(task_id).client.full_name + " [" + Task.find(task_id).client.id.to_s+"]"
end
end
row :user
row :user_role
row :task
row :score
row :comment
row :created_at
end
active_admin_comments
end
end
Here's my Gemfile:
source 'https://rubygems.org'
ruby "1.9.3"
gem 'rails'
gem 'pg' # Database (PostgreSQL)
group :assets do
gem 'sass-rails'
gem 'uglifier'
gem 'bourbon'
gem 'jquery-fileupload-rails'
gem 'jquery-rails'
gem 'asset_sync'
end
group :production do
gem 'honeybadger'
gem 'newrelic_rpm' # App monitoring
end
gem 'bcrypt-ruby' # Secure passwords
gem 'devise' # Authentication
gem 'unicorn' # Webserver
gem 'redis'# Session storage and Task applications
gem 'carrierwave' # File uploading
gem 'mini_magick' # Image processing
gem 'kaminari'# Pagination
gem 'turbolinks' # Pushstate and async loading
gem 'strong_parameters' # Attribute accessible replacement
gem 'dalli' # Memcached interface
gem 'fog' # CDN connector
gem 'activeadmin' # (user) administration
gem 'cancan' # Authorization
gem 'gibbon' # Mailchimp API wrapper
gem 'multi_json'
gem 'nokogiri' # HTML parser for error messages
gem 'pusher' # Hosted websockets
gem 'sidekiq' # Async processing
gem 'draper' # Decorator
gem 'lograge' # Improved logger
gem 'slim'
gem 'sinatra', :require => nil
group :development do
gem 'sqlite3'
gem 'better_errors'
gem 'binding_of_caller'
gem 'meta_request'
gem 'quiet_assets'
end
group :test, :darwin do
gem 'rb-fsevent'
end
group :test do
gem 'rspec-rails'
gem 'factory_girl_rails'
gem 'capybara'
# gem 'capybara-webkit'
gem 'database_cleaner'
gem 'launchy'
gem 'faker'
end
I encountered the same issue. I updated ActiveAdmin from 0.5.1. to 0.6.0. The inherited resource gem was also updated to 1.4.0.
Since ActiveAdmin requires 1.3.1 or higher, you can lock the version in your gem
gem 'inherited_resources', "1.3.1"
This solved the issue for me.

Rails 3.2.8 Actionmailer - stack level too deep PLUS uninitialized constant Mail::Ruby19

I'm following a tutorial referenced in a StackOverflow post for a simple contact form. Tutorial is here: http://matharvard.ca/posts/2011/aug/22/contact-form-in-rails-3/
Everything is copy/paste and change a few values for my domain and mail settings. Running into two very strange errors: When I first submit the contact form, I get a :
stack level too deep
Rails.root: /Users/me/application
Application Trace | Framework Trace | Full Trace
This error occurred while loading the following files:
mail
mail/parsers/rfc2822_obsolete
If I resubmit without restarting the rails app, I get:
uninitialized constant Mail::Ruby19
Rails.root: /Users/me/application
Application Trace | Framework Trace | Full Trace
app/mailers/notifications_mailer.rb:1:in `'
app/controllers/contact_controller.rb:11:in `create'
This error occurred while loading the following files:
mail
The sections I feel might be relavent are:
class NotificationsMailer < ActionMailer::Base
default :from => "feedback#mydomain.com"
default :to => "me#gmail.com"
def new_message(message)
#message = message
mail(:subject => "mydomain Feedback")
end
end
class Message
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
attr_accessor :name, :email, :subject, :body
validates :name, :email, :subject, :body, :presence => true
validates :email, :format => { :with => %r{.+#.+\..+} }, :allow_blank => true
def initialize(attributes = {})
attributes.each do |name, value|
send("#{name}=", value)
end
end
def persisted?
false
end
end
class ContactController < ApplicationController
def new
#message = Message.new
end
def create
#message = Message.new(params[:message])
if #message.valid?
NotificationsMailer.deliver_new_message params[:message]
# NotificationsMailer.new_message(#message).deliver
redirect_to(root_path, :notice => "Message was successfully sent.")
else
flash.now.alert = "Please fill all fields."
render :new
end
end
end
The rest of the templates are very straightforward and rendering fine.
Also, attaching the gemfile in case I missed some obvious conflict in there:
source 'https://rubygems.org'
gem 'rails', '3.2.8'
gem 'sqlite3'
gem 'redis', '2.1.1'
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
gem 'uglifier', '>= 1.0.3'
end
gem 'jquery-rails'
gem 'jade'
gem "haml", ">= 3.1.7"
gem "haml-rails", ">= 0.3.5", :group => :development
gem "hpricot", ">= 0.8.6", :group => :development
gem "ruby_parser", ">= 2.3.1", :group => :development
gem "rspec-rails", ">= 2.11.4", :group => [:development, :test]
gem "email_spec", ">= 1.2.1", :group => :test
gem "cucumber-rails", ">= 1.3.0", :group => :test, :require => false
gem "database_cleaner", ">= 0.9.1", :group => :test
gem "launchy", ">= 2.1.2", :group => :test
gem "capybara", ">= 1.1.2", :group => :test
gem "factory_girl_rails", ">= 4.1.0", :group => [:development, :test]
gem "bootstrap-sass", ">= 2.1.0.1"
gem "devise", ">= 2.1.2"
gem "cancan", ">= 1.6.8"
gem "rolify", ">= 3.2.0"
gem "simple_form", ">= 2.0.4"
gem "quiet_assets", ">= 1.0.1", :group => :development
gem "hub", ">= 1.10.2", :require => nil, :group => [:development]
gem 'paperclip'
gem 'aws-sdk'
gem 'aws-s3'
gem "high_voltage"
Thank you in advance.
I think the following code from ContactController.rb is the culprit:
NotificationsMailer.deliver_new_message params[:message]
You do not actually have a method name deliver_new_message
Try removing it, and uncommenting the line below it:
NotificationsMailer.new_message(#message).deliver
Hope this helps

Watchr doesn't use the colors from Turn

I'm using Minitest and the Turn gem for pretty test output. But when I run the tests in Watchr, none of the colors carry over. So I get all of the nice spacing and formatting, but none of the red/green color coding. Is there anything special I need to do to set this up?
Here's my .watchr file
# [PROGRESS|DOT]=true bundle exec watchr test/tests.watchr
ENV["WATCHR"] = "1"
system 'clear'
def growl(message)
if growlnotify = `which growlnotify`.chomp
title = "Somebody's Waaaatchin Meeeee...."
image = message.include?('0 failures, 0 errors') ? "~/.watchr_images/passed.png" : "~/.watchr_images/failed.png"
options = "-n Watchr --image '#{File.expand_path(image)}' -m '#{message}' '#{title}'"
system %(#{growlnotify} #{options} &)
end
end
def run(cmd)
puts(cmd)
`#{cmd}`
end
def run_test_file(file)
system('clear')
result = run(%Q(ruby -I"lib:test" -rubygems #{file}))
growl result.split("\n").last rescue nil
puts result
end
def run_all_tests
system('clear')
result = run "rake test"
growl result.split("\n").last rescue nil
puts result
end
def run_all_features
system('clear')
run "cucumber"
end
def related_test_files(path)
Dir['test/**/*.rb'].select { |file| file =~ /#{File.basename(path).split(".").first}_test.rb/ }
end
def run_suite
run_all_tests
run_all_features
end
watch('test/test_helper\.rb') { run_all_tests }
watch('test/.*/.*_test\.rb') { |m| run_test_file(m[0]) }
watch('app/.*/.*\.rb') { |m| related_test_files(m[0]).map {|tf| run_test_file(tf) } }
watch('lib/.*/.*\.rb') { |m| related_test_files(m[0]).map {|tf| run_test_file(tf) } }
watch('features/.*/.*\.feature') { run_all_features }
# Ctrl-\
Signal.trap 'QUIT' do
puts " --- Running all tests ---\n\n"
run_all_tests
end
#interrupted = false
# Ctrl-C
Signal.trap 'INT' do
if #interrupted then
#wants_to_quit = true
abort("\n")
else
puts "Interrupt a second time to quit"
#interrupted = true
Kernel.sleep 1.5
# raise Interrupt, nil # let the run loop catch it
run_suite
end
end
And my Gemfile, in case you care:
group :development do
# Deployment
gem 'sunspot_solr'
gem 'brakeman', :require=>false
# Capistrano requires...
gem 'net-scp', :require=>false
gem 'net-sftp', :require=>false
gem 'highline', :require=>false
gem 'net-ssh-gateway', :require=>false
gem 'capistrano', :require => false
gem 'capistrano_colors', :require => false
# Tests auto-run on save
gem 'guard', '~> 0.8.0'
gem 'guard-livereload'
gem 'watchr' # because guard doesn't reload my code
# Sugary goodness
gem 'annotate', :require => false, :git => 'git://github.com/jeremyolliver/annotate_models.git', :branch => 'rake_compatibility'
end
group :test do
# Mocking
gem 'vcr', :require=>false
gem 'webmock', :require=>false
# Testing framework
gem 'factory_girl_rails', :require => false
gem 'turn', '>= 0.9.3'
gem 'minitest' # At least v2.0.2 if using MiniShoulda.
gem 'guard-minitest'
gem 'mini_specunit' # The goods! Force MiniTest::Spec instead of MiniTest::Unit.
gem 'mini_backtrace' # Use Rails.backtrace_cleaner with MiniTest.
gem 'mini_shoulda' # A small Shoulda syntax on top of MiniTest::Spec.
gem 'mocha' # For the occasional black box test.
gem 'simplecov', :require => false # Test coverage in Ruby 1.9
# Spork makes our tests faster
gem 'spork', git: 'https://github.com/sporkrb/spork.git' #'~> 0.9.0.rc9'
gem 'guard-spork'
gem 'spork-testunit', git: 'git://github.com/sporkrb/spork-testunit.git'
gem 'ruby-prof' # needed to make Spork run on Minitest?
end

Resources