Agile Web Development with Rails, Edition 4

9.3 Iteration D3: Adding a button 9.1 Iteration D1: Finding a Cart

9.2 Iteration D2: Connecting Products to Carts

22 (tests|runs), 44 assertions, 0 failures, 0 errors.
<0> expected to be
>=
<1>.

Traceback:
  /home/rubys/git/awdwr/edition4/checkdepot.rb:38:in `assert_test_summary'
  /home/rubys/git/awdwr/edition4/checkdepot.rb:144:in `block in <class:DepotTest>'

Create line item which connects products to carts'

Create the model object.

rails generate scaffold LineItem product:references cart:belongs_to
      invoke  active_record
      create    db/migrate/20160127205359_create_line_items.rb
      create    app/models/line_item.rb
      invoke    test_unit
      create      test/models/line_item_test.rb
      create      test/fixtures/line_items.yml
      invoke  resource_route
       route    resources :line_items
      invoke  scaffold_controller
      create    app/controllers/line_items_controller.rb
      invoke    erb
      create      app/views/line_items
      create      app/views/line_items/index.html.erb
      create      app/views/line_items/edit.html.erb
      create      app/views/line_items/show.html.erb
      create      app/views/line_items/new.html.erb
      create      app/views/line_items/_form.html.erb
      invoke    test_unit
      create      test/controllers/line_items_controller_test.rb
      invoke    helper
      create      app/helpers/line_items_helper.rb
      invoke      test_unit
      invoke    jbuilder
      create      app/views/line_items/index.json.jbuilder
      create      app/views/line_items/show.json.jbuilder
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/line_items.coffee
      invoke    scss
      create      app/assets/stylesheets/line_items.scss
      invoke  scss
   identical    app/assets/stylesheets/scaffolds.scss
rails db:migrate
== 20160127205359 CreateLineItems: migrating ==================================
-- create_table(:line_items)
   -> 0.0068s
== 20160127205359 CreateLineItems: migrated (0.0068s) =========================
 

Cart has many line items.

edit app/models/cart.rb
class Cart < ApplicationRecord
  has_many :line_items, dependent: :destroy
end

Product has many line items.

edit app/models/product.rb
class Product < ApplicationRecord
  has_many :line_items
 
  before_destroy :ensure_not_referenced_by_any_line_item
 
  #...
 
 
  private
 
    # ensure that there are no line items referencing this product
    def ensure_not_referenced_by_any_line_item
      if line_items.empty?
        return true
      else
        errors.add(:base, 'Line Items present')
        return false
      end
    end
end

Line item belongs to both Cart and Product (But slightly more to the Cart). Also provide convenient access to the total price of the line item

edit app/models/line_item.rb
class LineItem < ApplicationRecord
  belongs_to :product
  belongs_to :cart
end
rake test:controllers
DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super. (called from included at /home/rubys/.rvm/gems/ruby-2.3.0/gems/turbolinks-2.5.3/lib/turbolinks/xhr_url_for.rb:7)
DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super. (called from included at /home/rubys/.rvm/gems/ruby-2.3.0/gems/turbolinks-2.5.3/lib/turbolinks/xhr_url_for.rb:7)
rails aborted!
ActiveRecord::StatementInvalid: SQLite3::SQLException: unrecognized token: "#": INSERT INTO "schema_migrations" (version) VALUES (#{v}), (#{v})
/home/rubys/.rvm/gems/ruby-2.3.0/gems/sqlite3-1.3.11/lib/sqlite3/database.rb:91:in `initialize'
/home/rubys/.rvm/gems/ruby-2.3.0/gems/sqlite3-1.3.11/lib/sqlite3/database.rb:91:in `new'
/home/rubys/.rvm/gems/ruby-2.3.0/gems/sqlite3-1.3.11/lib/sqlite3/database.rb:91:in `prepare'
/home/rubys/.rvm/gems/ruby-2.3.0/gems/sqlite3-1.3.11/lib/sqlite3/database.rb:134:in `execute'
/home/rubys/git/rails/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb:280:in `block in execute'
/home/rubys/git/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:528:in `block in log'
/home/rubys/git/rails/activesupport/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
/home/rubys/git/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:522:in `log'
/home/rubys/git/rails/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb:280:in `execute'
/home/rubys/git/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb:995:in `assume_migrated_upto_version'
/home/rubys/git/rails/activerecord/lib/active_record/schema.rb:52:in `define'
/home/rubys/git/rails/activerecord/lib/active_record/schema.rb:44:in `define'
/home/rubys/git/awdwr/edition4/work-230/depot/db/schema.rb:14:in `<top (required)>'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:296:in `load'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:296:in `block in load'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:268:in `load_dependency'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:296:in `load'
/home/rubys/git/rails/activerecord/lib/active_record/tasks/database_tasks.rb:216:in `load_schema'
/home/rubys/git/rails/activerecord/lib/active_record/railties/databases.rake:338:in `block (3 levels) in <top (required)>'
/home/rubys/git/rails/activerecord/lib/active_record/railties/databases.rake:327:in `block (3 levels) in <top (required)>'
/home/rubys/git/rails/activerecord/lib/active_record/railties/databases.rake:375:in `block (3 levels) in <top (required)>'
/home/rubys/git/rails/railties/lib/rails/commands/rake_proxy.rb:13:in `block in run_rake_task'
/home/rubys/git/rails/railties/lib/rails/commands/rake_proxy.rb:10:in `run_rake_task'
/home/rubys/git/rails/railties/lib/rails/commands/commands_tasks.rb:51:in `run_command!'
/home/rubys/git/rails/railties/lib/rails/command.rb:20:in `run'
/home/rubys/git/rails/railties/lib/rails/commands.rb:19:in `<top (required)>'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
SQLite3::SQLException: unrecognized token: "#"
/home/rubys/.rvm/gems/ruby-2.3.0/gems/sqlite3-1.3.11/lib/sqlite3/database.rb:91:in `initialize'
/home/rubys/.rvm/gems/ruby-2.3.0/gems/sqlite3-1.3.11/lib/sqlite3/database.rb:91:in `new'
/home/rubys/.rvm/gems/ruby-2.3.0/gems/sqlite3-1.3.11/lib/sqlite3/database.rb:91:in `prepare'
/home/rubys/.rvm/gems/ruby-2.3.0/gems/sqlite3-1.3.11/lib/sqlite3/database.rb:134:in `execute'
/home/rubys/git/rails/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb:280:in `block in execute'
/home/rubys/git/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:528:in `block in log'
/home/rubys/git/rails/activesupport/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
/home/rubys/git/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:522:in `log'
/home/rubys/git/rails/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb:280:in `execute'
/home/rubys/git/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb:995:in `assume_migrated_upto_version'
/home/rubys/git/rails/activerecord/lib/active_record/schema.rb:52:in `define'
/home/rubys/git/rails/activerecord/lib/active_record/schema.rb:44:in `define'
/home/rubys/git/awdwr/edition4/work-230/depot/db/schema.rb:14:in `<top (required)>'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:296:in `load'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:296:in `block in load'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:268:in `load_dependency'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:296:in `load'
/home/rubys/git/rails/activerecord/lib/active_record/tasks/database_tasks.rb:216:in `load_schema'
/home/rubys/git/rails/activerecord/lib/active_record/railties/databases.rake:338:in `block (3 levels) in <top (required)>'
/home/rubys/git/rails/activerecord/lib/active_record/railties/databases.rake:327:in `block (3 levels) in <top (required)>'
/home/rubys/git/rails/activerecord/lib/active_record/railties/databases.rake:375:in `block (3 levels) in <top (required)>'
/home/rubys/git/rails/railties/lib/rails/commands/rake_proxy.rb:13:in `block in run_rake_task'
/home/rubys/git/rails/railties/lib/rails/commands/rake_proxy.rb:10:in `run_rake_task'
/home/rubys/git/rails/railties/lib/rails/commands/commands_tasks.rb:51:in `run_command!'
/home/rubys/git/rails/railties/lib/rails/command.rb:20:in `run'
/home/rubys/git/rails/railties/lib/rails/commands.rb:19:in `<top (required)>'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
Tasks: TOP => db:test:load_schema
(See full trace by running task with --trace)
rake aborted!
ActiveRecord::PendingMigrationError: 

    
Migrations are pending. To resolve this issue, run:

    
	bin/rails db:migrate RAILS_ENV=test

    
/home/rubys/git/rails/activerecord/lib/active_record/migration.rb:578:in `check_pending!'
/home/rubys/git/rails/activerecord/lib/active_record/migration.rb:591:in `load_schema_if_pending!'
/home/rubys/git/rails/activerecord/lib/active_record/migration.rb:597:in `block in maintain_test_schema!'
/home/rubys/git/rails/activerecord/lib/active_record/migration.rb:828:in `suppress_messages'
/home/rubys/git/rails/activerecord/lib/active_record/migration.rb:602:in `method_missing'
/home/rubys/git/rails/activerecord/lib/active_record/migration.rb:597:in `maintain_test_schema!'
/home/rubys/git/rails/railties/lib/rails/test_help.rb:15:in `<top (required)>'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:302:in `require'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:302:in `block in require'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:268:in `load_dependency'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:302:in `require'
/home/rubys/git/awdwr/edition4/work-230/depot/test/test_helper.rb:3:in `<top (required)>'
/home/rubys/git/awdwr/edition4/work-230/depot/test/controllers/carts_controller_test.rb:1:in `require'
/home/rubys/git/awdwr/edition4/work-230/depot/test/controllers/carts_controller_test.rb:1:in `<top (required)>'
/home/rubys/git/rails/railties/lib/rails/test_unit/test_requirer.rb:11:in `require'
/home/rubys/git/rails/railties/lib/rails/test_unit/test_requirer.rb:11:in `block in require_files'
/home/rubys/git/rails/railties/lib/rails/test_unit/test_requirer.rb:10:in `each'
/home/rubys/git/rails/railties/lib/rails/test_unit/test_requirer.rb:10:in `require_files'
/home/rubys/git/rails/railties/lib/rails/test_unit/minitest_plugin.rb:75:in `plugin_rails_init'
/home/rubys/.rvm/gems/ruby-2.3.0@global/gems/minitest-5.8.3/lib/minitest.rb:74:in `block in init_plugins'
/home/rubys/.rvm/gems/ruby-2.3.0@global/gems/minitest-5.8.3/lib/minitest.rb:72:in `each'
/home/rubys/.rvm/gems/ruby-2.3.0@global/gems/minitest-5.8.3/lib/minitest.rb:72:in `init_plugins'
/home/rubys/.rvm/gems/ruby-2.3.0@global/gems/minitest-5.8.3/lib/minitest.rb:123:in `run'
/home/rubys/git/rails/railties/lib/rails/test_unit/minitest_plugin.rb:63:in `rake_run'
/home/rubys/git/rails/railties/lib/rails/test_unit/testing.rake:32:in `block (3 levels) in <top (required)>'
/home/rubys/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `eval'
/home/rubys/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => test:controllers
(See full trace by running task with --trace)

9.3 Iteration D3: Adding a button 9.1 Iteration D1: Finding a Cart