I have a Quote model in my rails app which has various attr types, some of which are being sent to / saved by the db, some are not and I cannot understand why. Please can you help me understand, thanks.
quotes_controller.rb
class QuotesController < ApplicationController
def create
#quote = Quote.new(quote_params)
if #quote.save
redirect_to root_url, notice: 'Quote request created'
else
render :new
end
end
private
def quote_params
params.require(:quote).permit(:gla, :prev_cover, :co_name, :postcode, :industry, :lives_overseas,
:scheme_start_date, :payment_frequency, :commision_level)
end
end
quote.rb model
class Quote < ApplicationRecord
validates :gla, presence: { message: "Must be selected" }
enum industry: [ :financial_services, :architect, :business_consultancy ]
enum payment_frequency: [ :annually, :monthly ]
end
schema.rb
create_table "quotes", force: :cascade do |t|
t.boolean "prev_cover"
t.string "co_name"
t.integer "co_number"
t.string "postcode"
t.string "industry"
t.boolean "lives_overseas"
t.date "scheme_start_date"
t.string "payment_frequency"
t.integer "commission_level"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "gla"
end
rails console:
Pry> Quote.last.attributes
Quote Load (0.4ms) SELECT "quotes".* FROM "quotes" ORDER BY "quotes"."id" DESC LIMIT $1 [["LIMIT", 1]]
=> {"id"=>6,
"prev_cover"=>true,
"co_name"=>"test1",
"co_number"=>nil,
"postcode"=>"al1 1aa",
"industry"=>nil,
"lives_overseas"=>true,
"scheme_start_date"=>Wed, 31 May 2017,
"payment_frequency"=>nil,
"commission_level"=>nil,
"created_at"=>Wed, 31 May 2017 19:23:07 UTC +00:00,
"updated_at"=>Wed, 31 May 2017 19:23:07 UTC +00:00,
"gla"=>true}
Stack Trace:
Started POST "/quotes" for 127.0.0.1 at 2017-05-31 21:04:37 +0100
Processing by QuotesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"ILAo0Bs9Wq9lrVPlM2e6+a1kioV9zbni9Uxd5Yt/QSLNY3aVWyJ4TsEUmXN62RWgbueHksr/yN6avwEm8v7bEQ==", "quote"=>{"gla"=>"1", "prev_cover"=>"true", "co_name"=>"testing1", "co_number"=>"123456", "postcode"=>"al1 1aa", "industry"=>"", "lives_overseas"=>"true", "scheme_start_date(1i)"=>"2017", "scheme_start_date(2i)"=>"5", "scheme_start_date(3i)"=>"31", "payment_frequency"=>"", "commission_level"=>"10"}, "commit"=>"Get quote"}
Unpermitted parameters: co_number, commission_level
(0.1ms) BEGIN
SQL (0.2ms) INSERT INTO "quotes" ("prev_cover", "co_name", "postcode", "lives_overseas", "scheme_start_date", "created_at", "updated_at", "gla") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id" [["prev_cover", "t"], ["co_name", "testing1"], ["postcode", "al1 1aa"], ["lives_overseas", "t"], ["scheme_start_date", "2017-05-31"], ["created_at", "2017-05-31 20:04:37.489368"], ["updated_at", "2017-05-31 20:04:37.489368"], ["gla", "t"]]
(0.3ms) COMMIT
Redirected to http://localhost:3000/
Completed 302 Found in 3ms (ActiveRecord: 0.6ms)
Started GET "/" for 127.0.0.1 at 2017-05-31 21:04:37 +0100
Processing by QuotesController#new as HTML
Rendering quotes/new.html.erb within layouts/application
Rendered quotes/new.html.erb within layouts/application (9.3ms)
Completed 200 OK in 34ms (Views: 32.7ms | ActiveRecord: 0.0ms)
From Rails console.
[1] pry(main)> Quote.last
Quote Load (0.2ms) SELECT "quotes".* FROM "quotes" ORDER BY "quotes"."id" DESC LIMIT $1 [["LIMIT", 1]]
=> #<Quote:0x007f951b14e918
id: 7,
prev_cover: true,
co_name: "testing1",
co_number: nil,
postcode: "al1 1aa",
industry: nil,
lives_overseas: true,
scheme_start_date: Wed, 31 May 2017,
payment_frequency: nil,
commission_level: nil,
created_at: Wed, 31 May 2017 20:04:37 UTC +00:00,
updated_at: Wed, 31 May 2017 20:04:37 UTC +00:00,
gla: true>
Ok stack trace and #toddmetheny help me sort out the missing or typo'd permitted attrs. Now just the enums Quote.industries and Quote.payment_frequencies whose values aren't getting saved.
Ok, so code changed to;
And this sends the attrs from the form, but they still don't get created in the db, stack trace:
Started POST "/quotes" for 127.0.0.1 at 2017-05-31 21:39:58 +0100
Processing by QuotesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"rkgX7CwrEHS/KnqG1C77mYkCiCEOWGshTxMCsbtGPdjGiDP20J4ccrAgplAGuKrdJyhECRWrsmXI0Ee9GNa6Zw==", "quote"=>{"gla"=>"1", "prev_cover"=>"true", "co_name"=>"halejulia", "co_number"=>"134532", "postcode"=>"al1 1aa", "industry"=>"financial_services", "lives_overseas"=>"true", "scheme_start_date(1i)"=>"2017", "scheme_start_date(2i)"=>"5", "scheme_start_date(3i)"=>"31", "payment_frequency"=>"monthly", "commission_level"=>"10"}, "commit"=>"Get quote"}
(0.1ms) BEGIN
SQL (0.3ms) INSERT INTO "quotes" ("prev_cover", "co_name", "co_number", "postcode", "industry", "lives_overseas", "scheme_start_date", "payment_frequency", "commission_level", "created_at", "updated_at", "gla") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) RETURNING "id" [["prev_cover", "t"], ["co_name", "halejulia"], ["co_number", 134532], ["postcode", "al1 1aa"], ["industry", 0], ["lives_overseas", "t"], ["scheme_start_date", "2017-05-31"], ["payment_frequency", 1], ["commission_level", 10], ["created_at", "2017-05-31 20:39:58.957674"], ["updated_at", "2017-05-31 20:39:58.957674"], ["gla", "t"]]
(0.3ms) COMMIT
Redirected to http://localhost:3000/
Completed 302 Found in 3ms (ActiveRecord: 0.7ms)
rails console :
Quote.last.payment_frequency
Quote Load (0.4ms) SELECT "quotes".* FROM "quotes" ORDER BY "quotes"."id" DESC LIMIT $1 [["LIMIT", 1]]
=> nil
So still the params aren't being persisted in the db, they're permitted params! any ideas?
Strange, a psql select * from .. query shows that the values have been saved, yet Rails console; Quote.last.payment_frequency returns nil???
Aha, i see that for a enum the column type should be integer, yet it is string in my model, could this be the issue perhaps?
Data type of enum'd attrs changed to integer and all behaves as expected.
Post the stack trace. co_number isn't whitelisted in the permitted params. So that's at least part of the issue with that particular field. The others are...but post what you're actually seeing in the logs so we can see what's being passed through the form. There will also be messages in the stack trace that give you clues as to why those values aren't saving.
Update: the stack trace lists 2 unpermitted parameters: co_number and commission_level (you have a typo permitting commission level and co_number isn't there)
A couple things have blank values, too...like payment_frequency and industry...I'd dig into why those things are blank if they shouldn't be. Does the form have values for those things? They aren't being passed. That seems to account for the rest of your nil values.
I am running tests that starts by logging in a user using the login form. It successfully opens Firefox, navigates to the login form and fills in the fields, but when it clicks the 'Login' button, it hangs indefinitely. When I quit Firefox the test that was running fails, but I am not getting any additional error message.
Any idea what I am doing wrong? This is driving me crazy and preventing me from testing a big portion of my app, any help would be much appreciated! Thanks!
Here is some additional info:
Rails 4.2.0
Ruby 2.2.4
Capybara 2.6.2
Selenium-Webdriver 2.53.0
Firefox 46.0.1
Using Capybara with RSpec
Capybara.javascript_driver = :selenium
/spec_helper.rb
require 'rubygems'
require 'spork'
#uncomment the following line to use spork with the debugger
#require 'spork/ext/ruby-debug'
Spork.prefork do
# Loading more in this block will cause your tests to run faster. However,
# if you change any configuration or code from libraries loaded here, you'll
# need to restart spork for it take effect.
require 'simplecov'
SimpleCov.start 'rails'
require 'capybara/rspec'
require 'aasm/rspec'
RSpec.configure do |config|
config.include Capybara::DSL
def test_sign_in(user)
# helper method to sign in the passed-in user
visit new_user_session_path
within ('#user-login-non-modal') do
within(".user-login") do
fill_in 'Email', with: user.email
fill_in 'Password', with: 'foobar11'
click_button 'Sign in'
end
end
end
def test_partner_sign_in(partner)
# helper method to sign in the passed-in partner
visit new_partner_session_path
within('#partner-login-non-modal') do
within(".partner-login") do
fill_in 'Email', with: partner.email
fill_in 'Password', with: 'foobar11'
click_button 'Sign in'
end
end
end
# configure database cleaner
config.before(:suite) do
DatabaseCleaner.start
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each, js: true) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
# Many RSpec users commonly either run the entire suite or an individual
# file, and it's useful to allow more verbose output when running an
# individual spec file.
# if config.files_to_run.one?
# Use the documentation formatter for detailed output,
# unless a formatter has already been configured
# (e.g. via a command-line flag).
# config.default_formatter = 'doc'
# end
end
end
Spork.each_run do
# This code will be run each time you run your specs.
end
/rails_helper.rb
# This file is copied to spec/ when you run 'rails generate
rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'devise'
require 'controller_macros'
require 'byebug'
# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.include Devise::TestHelpers, :type => :controller
config.include Rails.application.routes.url_helpers
config.extend ControllerMacros, :type => :controller
# The different available types are documented in the features, such as in
# https://relishapp.com/rspec/rspec-rails/docs
config.infer_spec_type_from_file_location!
end
/test.rb
Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.
# The test environment is used exclusively to run your application's
# test suite. You never need to work with it otherwise. Remember that
# your test database is "scratch space" for the test suite and is wiped
# and recreated between test runs. Don't rely on the data there!
config.cache_classes = true
# Do not eager load code on boot. This avoids loading your whole application
# just for the purpose of running a single test. If you are using a tool that
# preloads Rails for running tests, you may have to set it to true.
config.eager_load = false
# Configure static asset server for tests with Cache-Control for performance.
config.serve_static_files = true
config.static_cache_control = 'public, max-age=3600'
# Show full error reports and disable caching.
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
# For Devise
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
# Raise exceptions instead of rendering exception templates.
config.action_dispatch.show_exceptions = false
# Disable request forgery protection in test environment.
config.action_controller.allow_forgery_protection = false
# Tell Action Mailer not to deliver emails to the real world.
# The :test delivery method accumulates sent emails in the
# ActionMailer::Base.deliveries array.
config.action_mailer.delivery_method = :test
# Print deprecation notices to the stderr.
config.active_support.deprecation = :stderr
# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true
end
.rspec
--color
--require spec_helper
--drb
Edit:
user_pages_spec.rb
describe "show page" do
before do
# #user = FactoryGirl.create(:user)
#user = User.invite!(:email => "test_user#example.com", :skip_invitation => true)
#user.accept_invitation!
#user.password = "foobar11"
#user.first_name = "John"
#user.last_name = "Smith"
#user.save
#car1 = FactoryGirl.create(:car, user: #user)
#car2 = FactoryGirl.create(:car, user: #user)
#tomorrow = Time.now + 24*60*60
#earlier_today = Time.now - 30
#app_in_future1 = FactoryGirl.create(:appointment, car: #car1, garage: #car1.garage, pickuptime: #tomorrow)
#app_in_future2 = FactoryGirl.create(:appointment, car: #car1, garage: #car1.garage, pickuptime: #tomorrow + 24*60*60)
#app_in_past = FactoryGirl.build(:appointment, car: #car1, garage: #car1.garage, pickuptime: #earlier_today)
#app_in_past.save(validate: false)
different_pickup_time = #tomorrow + 60*60
#different_user = FactoryGirl.create(:user)
#car_different_user = FactoryGirl.create(:car, make: "diff_make", user: #different_user)
#app_different_user = FactoryGirl.create(:appointment, car: #car_different_user, garage: #car_different_user.garage, pickuptime: different_pickup_time)
test_sign_in(#user)
end
describe "adding an appointment to a car", js:true do
context "when the appointment is in the future" do
it "should increment the number of the car's appointments" do
expect do
within(".create-appt-row-#{#car1.id}") do
click_button 'Add Appointment'
fill_in 'appointment_pickuptime', with: "12/07/2100 4:57 PM"
click_button 'Book It'
end
end.to change(#car1.appointments, :count).by(1)
end
end
end
Edit 3:
After updating to selenium-webdriver 2.53.4 and re-running the test that requires the user-login, Firefox is hanging indefinitely, and when I quit Firefox manually I get this error message with the failing test:
Failure/Error: visit new_user_session_path
Errno::ECONNREFUSED:
Connection refused - connect(2) for "127.0.0.1" port 7055
# /Users/JAckerman/.rvm/gems/ruby-2.2.4/gems/selenium-webdriver-2.53.4/lib/selenium/webdriver/remote/http/default.rb:107:in `response_for'
# /Users/JAckerman/.rvm/gems/ruby-2.2.4/gems/selenium-webdriver-2.53.4/lib/selenium/webdriver/remote/http/default.rb:58:in `request'
# /Users/JAckerman/.rvm/gems/ruby-2.2.4/gems/selenium-webdriver-2.53.4/lib/selenium/webdriver/remote/http/common.rb:59:in `call'
# /Users/JAckerman/.rvm/gems/ruby-2.2.4/gems/selenium-webdriver-2.53.4/lib/selenium/webdriver/remote/bridge.rb:649:in `raw_execute'
# /Users/JAckerman/.rvm/gems/ruby-2.2.4/gems/selenium-webdriver-2.53.4/lib/selenium/webdriver/remote/bridge.rb:627:in `execute'
# /Users/JAckerman/.rvm/gems/ruby-2.2.4/gems/selenium-webdriver-2.53.4/lib/selenium/webdriver/remote/bridge.rb:134:in `get'
# /Users/JAckerman/.rvm/gems/ruby-2.2.4/gems/selenium-webdriver-2.53.4/lib/selenium/webdriver/common/navigation.rb:33:in `to'
# /Users/JAckerman/.rvm/gems/ruby-2.2.4/gems/capybara-2.6.2/lib/capybara/selenium/driver.rb:45:in `visit'
# /Users/JAckerman/.rvm/gems/ruby-2.2.4/gems/capybara-2.6.2/lib/capybara/session.rb:232:in `visit'
# /Users/JAckerman/.rvm/gems/ruby-2.2.4/gems/capybara-2.6.2/lib/capybara/dsl.rb:51:in `block (2 levels) in <module:DSL>'
# ./spec/spec_helper.rb:23:in `test_sign_in'
# ./spec/requests/user_pages_spec.rb:31:in `block (3 levels) in <top (required)>'
# /Users/JAckerman/.rvm/gems/ruby-2.2.4/bundler/gems/spork-224df492657e/lib/spork/test_framework/rspec.rb:12:in `run_tests'
# /Users/JAckerman/.rvm/gems/ruby-2.2.4/bundler/gems/spork-224df492657e/lib/spork/run_strategy/forking.rb:13:in `block in run'
# /Users/JAckerman/.rvm/gems/ruby-2.2.4/bundler/gems/spork-224df492657e/lib/spork/forker.rb:21:in `block in initialize'
# /Users/JAckerman/.rvm/gems/ruby-2.2.4/bundler/gems/spork-224df492657e/lib/spork/forker.rb:18:in `fork'
# /Users/JAckerman/.rvm/gems/ruby-2.2.4/bundler/gems/spork-224df492657e/lib/spork/forker.rb:18:in `initialize'
# /Users/JAckerman/.rvm/gems/ruby-2.2.4/bundler/gems/spork-224df492657e/lib/spork/run_strategy/forking.rb:9:in `new'
# /Users/JAckerman/.rvm/gems/ruby-2.2.4/bundler/gems/spork-224df492657e/lib/spork/run_strategy/forking.rb:9:in `run'
# /Users/JAckerman/.rvm/gems/ruby-2.2.4/bundler/gems/spork-224df492657e/lib/spork/server.rb:49:in `run'
Edit 4:
test.log after running the test that hangs
[1m[36mActiveRecord::SchemaMigration Load (0.6ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
[1m[35m (0.2ms)[0m BEGIN
[1m[36m (0.2ms)[0m [1mCOMMIT[0m
[1m[35m (0.1ms)[0m BEGIN
[1m[36m (1.7ms)[0m [1mALTER TABLE "active_admin_comments" DISABLE TRIGGER ALL;ALTER TABLE "authorizations" DISABLE TRIGGER ALL;ALTER TABLE "authorized_drivers" DISABLE TRIGGER ALL;ALTER TABLE "garagepartners" DISABLE TRIGGER ALL;ALTER TABLE "cars" DISABLE TRIGGER ALL;ALTER TABLE "locations" DISABLE TRIGGER ALL;ALTER TABLE "partners" DISABLE TRIGGER ALL;ALTER TABLE "appointments" DISABLE TRIGGER ALL;ALTER TABLE "images" DISABLE TRIGGER ALL;ALTER TABLE "garages" DISABLE TRIGGER ALL;ALTER TABLE "users" DISABLE TRIGGER ALL;ALTER TABLE "schema_migrations" DISABLE TRIGGER ALL;ALTER TABLE "companies" DISABLE TRIGGER ALL;ALTER TABLE "damages" DISABLE TRIGGER ALL;ALTER TABLE "admin_users" DISABLE TRIGGER ALL;ALTER TABLE "delayed_jobs" DISABLE TRIGGER ALL[0m
[1m[35m (2.3ms)[0m SELECT schemaname || '.' || tablename
FROM pg_tables
WHERE
tablename !~ '_prt_' AND
tablename <> 'schema_migrations' AND
schemaname = ANY (current_schemas(false))
[1m[36m (2.0ms)[0m [1mselect table_name from information_schema.views where table_schema = 'parkme3_test'[0m
[1m[35m (55.4ms)[0m TRUNCATE TABLE "public"."active_admin_comments", "public"."authorizations", "public"."authorized_drivers", "public"."garagepartners", "public"."cars", "public"."locations", "public"."partners", "public"."appointments", "public"."images", "public"."garages", "public"."users", "public"."companies", "public"."damages", "public"."admin_users", "public"."delayed_jobs" RESTART IDENTITY CASCADE;
[1m[36m (0.9ms)[0m [1mALTER TABLE "active_admin_comments" ENABLE TRIGGER ALL;ALTER TABLE "authorizations" ENABLE TRIGGER ALL;ALTER TABLE "authorized_drivers" ENABLE TRIGGER ALL;ALTER TABLE "garagepartners" ENABLE TRIGGER ALL;ALTER TABLE "schema_migrations" ENABLE TRIGGER ALL;ALTER TABLE "cars" ENABLE TRIGGER ALL;ALTER TABLE "locations" ENABLE TRIGGER ALL;ALTER TABLE "partners" ENABLE TRIGGER ALL;ALTER TABLE "appointments" ENABLE TRIGGER ALL;ALTER TABLE "images" ENABLE TRIGGER ALL;ALTER TABLE "garages" ENABLE TRIGGER ALL;ALTER TABLE "users" ENABLE TRIGGER ALL;ALTER TABLE "companies" ENABLE TRIGGER ALL;ALTER TABLE "damages" ENABLE TRIGGER ALL;ALTER TABLE "admin_users" ENABLE TRIGGER ALL;ALTER TABLE "delayed_jobs" ENABLE TRIGGER ALL[0m
[1m[35mUser Load (1.4ms)[0m SELECT "users".* FROM "users" WHERE "users"."email" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["email", "test_user#example.com"]]
[1m[36mUser Load (0.6ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."invitation_token" = $1 ORDER BY "users"."id" ASC LIMIT 1[0m [["invitation_token", "12f7e3e1ca7f4be3bf7ebc51410444e1b49d703cade994df75ca75a2e49fed3e"]]
[1m[35m (0.2ms)[0m SAVEPOINT active_record_1
[1m[36mSQL (0.7ms)[0m [1mINSERT INTO "users" ("receive_email_notification", "receive_text_notification", "email", "encrypted_password", "invitation_token", "invitation_created_at", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id"[0m [["receive_email_notification", "t"], ["receive_text_notification", "t"], ["email", "test_user#example.com"], ["encrypted_password", "$2a$04$dzJj7097jsrmaWOfVGqrq.F4wcKx4yktACtDVjI7IXbt6u83C7.QO"], ["invitation_token", "12f7e3e1ca7f4be3bf7ebc51410444e1b49d703cade994df75ca75a2e49fed3e"], ["invitation_created_at", "2016-07-15 15:02:23.921974"], ["created_at", "2016-07-15 15:02:23.923749"], ["updated_at", "2016-07-15 15:02:23.923749"]]
[1m[35m (0.2ms)[0m RELEASE SAVEPOINT active_record_1
[1m[36m (0.3ms)[0m [1mSAVEPOINT active_record_1[0m
[1m[35mSQL (0.5ms)[0m UPDATE "users" SET "invitation_accepted_at" = $1, "invitation_token" = $2, "updated_at" = $3 WHERE "users"."id" = $4 [["invitation_accepted_at", "2016-07-15 15:02:23.929347"], ["invitation_token", nil], ["updated_at", "2016-07-15 15:02:23.930593"], ["id", 1]]
[1m[36m (0.2ms)[0m [1mRELEASE SAVEPOINT active_record_1[0m
[1m[35m (0.1ms)[0m SAVEPOINT active_record_1
[1m[36mSQL (0.5ms)[0m [1mUPDATE "users" SET "encrypted_password" = $1, "first_name" = $2, "last_name" = $3, "updated_at" = $4 WHERE "users"."id" = $5[0m [["encrypted_password", "$2a$04$c8LSFWcTJXE7np2W838iLui3lHt7yYXSjQvhQoxt91EqkW1xvBr1q"], ["first_name", "John"], ["last_name", "Smith"], ["updated_at", "2016-07-15 15:02:23.938120"], ["id", 1]]
[1m[35m (0.3ms)[0m RELEASE SAVEPOINT active_record_1
[1m[36m (0.2ms)[0m [1mSAVEPOINT active_record_1[0m
[1m[35mSQL (0.5ms)[0m INSERT INTO "companies" ("name", "min_time_in_minutes", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["name", "company_1"], ["min_time_in_minutes", 15], ["created_at", "2016-07-15 15:02:23.977122"], ["updated_at", "2016-07-15 15:02:23.977122"]]
[1m[36m (0.3ms)[0m [1mRELEASE SAVEPOINT active_record_1[0m
[1m[35m (0.2ms)[0m SAVEPOINT active_record_1
[1m[36mSQL (0.6ms)[0m [1mINSERT INTO "garages" ("name", "time_zone", "min_time_in_minutes", "urgent_minutes", "use_acknowledgement", "use_locations", "use_damage_tracking", "use_numpad_make_appointment_form", "phone_number", "company_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) RETURNING "id"[0m [["name", "garage_1"], ["time_zone", "Eastern Time (US & Canada)"], ["min_time_in_minutes", 15], ["urgent_minutes", 60], ["use_acknowledgement", "t"], ["use_locations", "t"], ["use_damage_tracking", "t"], ["use_numpad_make_appointment_form", "f"], ["phone_number", "7777777777"], ["company_id", 1], ["created_at", "2016-07-15 15:02:23.982258"], ["updated_at", "2016-07-15 15:02:23.982258"]]
[1m[35m (0.2ms)[0m RELEASE SAVEPOINT active_record_1
[1m[36m (0.2ms)[0m [1mSAVEPOINT active_record_1[0m
[1m[35mSQL (0.7ms)[0m INSERT INTO "cars" ("make", "car_model", "year", "car_number", "garage_id", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id" [["make", "make_1"], ["car_model", "car_model_1"], ["year", 2014], ["car_number", 1], ["garage_id", 1], ["user_id", 1], ["created_at", "2016-07-15 15:02:23.988597"], ["updated_at", "2016-07-15 15:02:23.988597"]]
[1m[36m (0.2ms)[0m [1mRELEASE SAVEPOINT active_record_1[0m
[1m[35m (0.2ms)[0m SAVEPOINT active_record_1
[1m[36mSQL (0.3ms)[0m [1mINSERT INTO "companies" ("name", "min_time_in_minutes", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"[0m [["name", "company_2"], ["min_time_in_minutes", 15], ["created_at", "2016-07-15 15:02:23.996186"], ["updated_at", "2016-07-15 15:02:23.996186"]]
[1m[35m (0.2ms)[0m RELEASE SAVEPOINT active_record_1
[1m[36m (0.2ms)[0m [1mSAVEPOINT active_record_1[0m
[1m[35mSQL (0.4ms)[0m INSERT INTO "garages" ("name", "time_zone", "min_time_in_minutes", "urgent_minutes", "use_acknowledgement", "use_locations", "use_damage_tracking", "use_numpad_make_appointment_form", "phone_number", "company_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) RETURNING "id" [["name", "garage_2"], ["time_zone", "Eastern Time (US & Canada)"], ["min_time_in_minutes", 15], ["urgent_minutes", 60], ["use_acknowledgement", "t"], ["use_locations", "t"], ["use_damage_tracking", "t"], ["use_numpad_make_appointment_form", "f"], ["phone_number", "7777777777"], ["company_id", 2], ["created_at", "2016-07-15 15:02:23.999804"], ["updated_at", "2016-07-15 15:02:23.999804"]]
[1m[36m (0.2ms)[0m [1mRELEASE SAVEPOINT active_record_1[0m
[1m[35m (0.1ms)[0m SAVEPOINT active_record_1
[1m[36mSQL (0.4ms)[0m [1mINSERT INTO "cars" ("make", "car_model", "year", "car_number", "garage_id", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id"[0m [["make", "make_2"], ["car_model", "car_model_2"], ["year", 2014], ["car_number", 2], ["garage_id", 2], ["user_id", 1], ["created_at", "2016-07-15 15:02:24.003820"], ["updated_at", "2016-07-15 15:02:24.003820"]]
[1m[35m (0.2ms)[0m RELEASE SAVEPOINT active_record_1
[1m[36m (0.2ms)[0m [1mSAVEPOINT active_record_1[0m
[1m[35mSQL (0.6ms)[0m INSERT INTO "appointments" ("aasm_state", "pickuptime", "car_id", "garage_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["aasm_state", "scheduled"], ["pickuptime", "2016-07-16 15:02:24.006227"], ["car_id", 1], ["garage_id", 1], ["created_at", "2016-07-15 15:02:24.026603"], ["updated_at", "2016-07-15 15:02:24.026603"]]
[1m[36m (0.2ms)[0m [1mRELEASE SAVEPOINT active_record_1[0m
[1m[35m (0.2ms)[0m SAVEPOINT active_record_1
[1m[36mSQL (0.4ms)[0m [1mINSERT INTO "appointments" ("aasm_state", "pickuptime", "car_id", "garage_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"[0m [["aasm_state", "scheduled"], ["pickuptime", "2016-07-17 15:02:24.006227"], ["car_id", 1], ["garage_id", 1], ["created_at", "2016-07-15 15:02:24.033088"], ["updated_at", "2016-07-15 15:02:24.033088"]]
[1m[35m (0.3ms)[0m RELEASE SAVEPOINT active_record_1
[1m[36m (0.2ms)[0m [1mSAVEPOINT active_record_1[0m
[1m[35mSQL (0.4ms)[0m INSERT INTO "appointments" ("aasm_state", "pickuptime", "car_id", "garage_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["aasm_state", "scheduled"], ["pickuptime", "2016-07-15 15:01:54.006233"], ["car_id", 1], ["garage_id", 1], ["created_at", "2016-07-15 15:02:24.039517"], ["updated_at", "2016-07-15 15:02:24.039517"]]
[1m[36m (0.3ms)[0m [1mRELEASE SAVEPOINT active_record_1[0m
[1m[35m (0.2ms)[0m SAVEPOINT active_record_1
[1m[36mUser Exists (0.5ms)[0m [1mSELECT 1 AS one FROM "users" WHERE "users"."email" = 'person_1#example.com' LIMIT 1[0m
[1m[35mSQL (0.4ms)[0m INSERT INTO "users" ("receive_email_notification", "receive_text_notification", "email", "encrypted_password", "invitation_accepted_at", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id" [["receive_email_notification", "t"], ["receive_text_notification", "t"], ["email", "person_1#example.com"], ["encrypted_password", "$2a$04$glYjOuWxkRp06jc/l1X0ceeI5OD.eqWMzsnt/MRhizfX7RuD7ti9m"], ["invitation_accepted_at", "2016-07-15 14:02:24.045381"], ["created_at", "2016-07-15 15:02:24.053170"], ["updated_at", "2016-07-15 15:02:24.053170"]]
[1m[36m (0.2ms)[0m [1mRELEASE SAVEPOINT active_record_1[0m
[1m[35m (0.2ms)[0m SAVEPOINT active_record_1
[1m[36mSQL (0.3ms)[0m [1mINSERT INTO "companies" ("name", "min_time_in_minutes", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"[0m [["name", "company_3"], ["min_time_in_minutes", 15], ["created_at", "2016-07-15 15:02:24.060156"], ["updated_at", "2016-07-15 15:02:24.060156"]]
[1m[35m (0.3ms)[0m RELEASE SAVEPOINT active_record_1
[1m[36m (0.1ms)[0m [1mSAVEPOINT active_record_1[0m
[1m[35mSQL (0.4ms)[0m INSERT INTO "garages" ("name", "time_zone", "min_time_in_minutes", "urgent_minutes", "use_acknowledgement", "use_locations", "use_damage_tracking", "use_numpad_make_appointment_form", "phone_number", "company_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) RETURNING "id" [["name", "garage_3"], ["time_zone", "Eastern Time (US & Canada)"], ["min_time_in_minutes", 15], ["urgent_minutes", 60], ["use_acknowledgement", "t"], ["use_locations", "t"], ["use_damage_tracking", "t"], ["use_numpad_make_appointment_form", "f"], ["phone_number", "7777777777"], ["company_id", 3], ["created_at", "2016-07-15 15:02:24.063107"], ["updated_at", "2016-07-15 15:02:24.063107"]]
[1m[36m (0.3ms)[0m [1mRELEASE SAVEPOINT active_record_1[0m
[1m[35m (0.1ms)[0m SAVEPOINT active_record_1
[1m[36mSQL (0.3ms)[0m [1mINSERT INTO "cars" ("make", "car_model", "year", "car_number", "garage_id", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id"[0m [["make", "diff_make"], ["car_model", "car_model_3"], ["year", 2014], ["car_number", 3], ["garage_id", 3], ["user_id", 2], ["created_at", "2016-07-15 15:02:24.066940"], ["updated_at", "2016-07-15 15:02:24.066940"]]
[1m[35m (0.2ms)[0m RELEASE SAVEPOINT active_record_1
[1m[36m (0.2ms)[0m [1mSAVEPOINT active_record_1[0m
[1m[35mSQL (0.4ms)[0m INSERT INTO "appointments" ("aasm_state", "pickuptime", "car_id", "garage_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["aasm_state", "scheduled"], ["pickuptime", "2016-07-16 16:02:24.006227"], ["car_id", 3], ["garage_id", 3], ["created_at", "2016-07-15 15:02:24.071953"], ["updated_at", "2016-07-15 15:02:24.071953"]]
[1m[36m (0.2ms)[0m [1mRELEASE SAVEPOINT active_record_1[0m
Started GET "/login" for 127.0.0.1 at 2016-07-15 11:02:26 -0400
Processing by Users::SessionsController#new as HTML
Rendered users/sessions/_new.html.erb (75.4ms)
Rendered users/sessions/new.html.erb within layouts/application (82.6ms)
Rendered layouts/_shim.html.erb (0.2ms)
Rendered application/_favicon.html.erb (1.2ms)
Rendered users/sessions/_new.html.erb (8.2ms)
Rendered partners/sessions/_new.html.erb (16.3ms)
Rendered layouts/_header.html.erb (27.6ms)
Rendered layouts/_footer.html.erb (1.2ms)
Rendered layouts/_google_analytics.html.erb (1.0ms)
Completed 200 OK in 206ms (Views: 190.7ms | ActiveRecord: 0.0ms)
Started POST "/login" for 127.0.0.1 at 2016-07-15 11:02:28 -0400
Processing by Users::SessionsController#create as HTML
Parameters: {"utf8"=>"✓", "user"=>{"email"=>"test_user#example.com", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Sign in"}
This is where it hung. The rest of the log appeared after the test timed out.
[1m[35m (0.5ms)[0m ALTER TABLE "active_admin_comments" DISABLE TRIGGER ALL;ALTER TABLE "authorizations" DISABLE TRIGGER ALL;ALTER TABLE "authorized_drivers" DISABLE TRIGGER ALL;ALTER TABLE "garagepartners" DISABLE TRIGGER ALL;ALTER TABLE "schema_migrations" DISABLE TRIGGER ALL;ALTER TABLE "cars" DISABLE TRIGGER ALL;ALTER TABLE "locations" DISABLE TRIGGER ALL;ALTER TABLE "partners" DISABLE TRIGGER ALL;ALTER TABLE "appointments" DISABLE TRIGGER ALL;ALTER TABLE "images" DISABLE TRIGGER ALL;ALTER TABLE "garages" DISABLE TRIGGER ALL;ALTER TABLE "users" DISABLE TRIGGER ALL;ALTER TABLE "companies" DISABLE TRIGGER ALL;ALTER TABLE "damages" DISABLE TRIGGER ALL;ALTER TABLE "admin_users" DISABLE TRIGGER ALL;ALTER TABLE "delayed_jobs" DISABLE TRIGGER ALL
[1m[36m (45.9ms)[0m [1mTRUNCATE TABLE "public"."active_admin_comments", "public"."authorizations", "public"."authorized_drivers", "public"."garagepartners", "public"."cars", "public"."locations", "public"."partners", "public"."appointments", "public"."images", "public"."garages", "public"."users", "public"."companies", "public"."damages", "public"."admin_users", "public"."delayed_jobs" RESTART IDENTITY CASCADE;[0m
[1m[35m (0.8ms)[0m ALTER TABLE "active_admin_comments" ENABLE TRIGGER ALL;ALTER TABLE "authorizations" ENABLE TRIGGER ALL;ALTER TABLE "authorized_drivers" ENABLE TRIGGER ALL;ALTER TABLE "garagepartners" ENABLE TRIGGER ALL;ALTER TABLE "schema_migrations" ENABLE TRIGGER ALL;ALTER TABLE "cars" ENABLE TRIGGER ALL;ALTER TABLE "locations" ENABLE TRIGGER ALL;ALTER TABLE "partners" ENABLE TRIGGER ALL;ALTER TABLE "appointments" ENABLE TRIGGER ALL;ALTER TABLE "images" ENABLE TRIGGER ALL;ALTER TABLE "garages" ENABLE TRIGGER ALL;ALTER TABLE "users" ENABLE TRIGGER ALL;ALTER TABLE "companies" ENABLE TRIGGER ALL;ALTER TABLE "damages" ENABLE TRIGGER ALL;ALTER TABLE "admin_users" ENABLE TRIGGER ALL;ALTER TABLE "delayed_jobs" ENABLE TRIGGER ALL
This was in the Spork console after the test timed out:
1.1) Failure/Error: click_button 'Sign in'
Net::ReadTimeout:
Net::ReadTimeout
users/sessions_controller.rb (using Devise)
class Users::SessionsController < Devise::SessionsController
include ApplicationHelper
def create
super
end
def new
render 'new'
end
def after_sign_in_path_for(resource)
user_path(resource)
end
end
Got stuck at this point when stepping through the controller using byebug:
in /Users/JAckerman/.rvm/gems/ruby-2.2.4/gems/devise-3.5.6/app/controllers/devise/sessions_controller.rb
12: respond_with(resource, serialize_options(resource))
13: end
14:
15: # POST /resource/sign_in
16: def create
=> 17: self.resource = warden.authenticate!(auth_options)
After stepping into that, I got stuck here:
in /Users/JAckerman/.rvm/gems/ruby-2.2.4/gems/warden-1.2.6/lib/warden/proxy.rb
122: # Example
123: # env['warden'].authenticate!(:password, :scope => :publisher) # throws if it cannot authenticate
124: #
125: # :api: public
126: def authenticate!(*args)
=> 127: user, opts = _perform_authentication(*args)
128: throw(:warden, opts) unless user
129: user
130: end
After pulling out a lot of hair, this is what ended up working:
Change:
# configure database cleaner
config.before(:suite) do
DatabaseCleaner.start
DatabaseCleaner.clean_with(:truncation)
end
To:
# configure database cleaner
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
And add config.use_transactional_fixtures = false to rails_helper.rb, which had been there at the beginning and had been removed at some point in the many iterations of configurations that I had tried.
Either way, hopefully this saves someone else a headache!
I am writing an app to help me keep track of my social media advertising budgets. When you enter a new advert it should calculate and update the amount spent on the budget it is drawing from. Here is my model that achieves that.
class Advert < ActiveRecord::Base
belongs_to :budget
before_save :update_budget
after_destroy :update_budget
validates :budget_id, :name, :platform, :ad_type, :amount, :start_date, :end_date, presence: true
validates :amount, numericality: true
validate :check_budget
# Checks to see if there is enough budget remaining to set up advert
def check_budget
if self.amount > self.budget.amount_remaining
errors.add(:amount, " cannot exceed amount remaining in budget.")
end
end
# Updates the amount remaining in the budget on advert save.
def update_budget
budget = Budget.find(self.budget_id)
#adverts = Advert.all
total_spent = self.amount
#adverts.each do |advert|
if advert.budget_id == self.budget_id
total_spent += advert.amount
end
end
budget.amount_spent = total_spent
budget.save
end
end
This all works but I am currently teaching myself to write tests so I thought I would write a test in rspec for it.
require 'rails_helper'
describe Advert do
it "updates budget before save" do
advert = create(:advert)
budget = advert.budget
expect(budget.amount_spent).to eq(advert.amount)
expect(budget.amount_remaining).to eq(budget.amount - budget.amount_spent)
end
end
However, this test if failing but I cannot figure out why. Here is the error code.
1) Advert updates budget before save
Failure/Error: expect(budget.amount_spent).to eq(advert.amount)
expected: 7.0 (#<BigDecimal:7ffa61358b18,'0.7E1',9(18)>)
got: 0.0 (#<BigDecimal:7ffa6026a9a0,'0.0',9(18)>)
(compared using ==)
# ./spec/models/advert_spec.rb:27:in `block (2 levels) in <top (required)>'
And here is the relevant test log.
SQL (0.3ms) INSERT INTO "budgets" ("name", "amount", "client_id", "amount_remaining", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["name", "eos"], ["amount", "432.0"], ["client_id", 102], ["amount_remaining", "432.0"], ["created_at", "2016-03-12 18:08:54.607999"], ["updated_at", "2016-03-12 18:08:54.607999"]]
(0.1ms) RELEASE SAVEPOINT active_record_1
(0.2ms) SAVEPOINT active_record_1
Budget Load (0.4ms) SELECT "budgets".* FROM "budgets" WHERE "budgets"."id" = $1 LIMIT 1 [["id", 49]]
Advert Load (0.5ms) SELECT "adverts".* FROM "adverts"
SQL (0.4ms) UPDATE "budgets" SET "amount_spent" = $1, "amount_remaining" = $2, "updated_at" = $3 WHERE "budgets"."id" = $4 [["amount_spent", "7.0"], ["amount_remaining", "425.0"], ["updated_at", "2016-03-12 18:08:54.616491"], ["id", 49]]
SQL (0.4ms) INSERT INTO "adverts" ("budget_id", "name", "platform", "ad_type", "amount", "start_date", "end_date", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING "id" [["budget_id", 49], ["name", "ut"], ["platform", "voluptate"], ["ad_type", "facere"], ["amount", "7.0"], ["start_date", "2016-03-01"], ["end_date", "2016-04-12"], ["created_at", "2016-03-12 18:08:54.619698"], ["updated_at", "2016-03-12 18:08:54.619698"]]
(0.2ms) RELEASE SAVEPOINT active_record_1
(0.2ms) ROLLBACK
Interestingly if I comment out the first 'expect' the test passes. It's as though it cannot access advert.amount so set's it as 0.
Anyone have any ideas?
This solved my issue.
describe Advert do
it "updates budget before save" do
advert = build(:advert)
budget = advert.budget
expect(budget.amount_spent).to eq(0)
advert.save
budget.reload
expect(budget.amount_spent).to eq(advert.amount)
expect(budget.amount_remaining).to eq(budget.amount - budget.amount_spent)
end
I think the source of my problem was not reloading my budget which meant that I was trying to access the attribute before it had been updated.