modifying a relation which is a resource

Create an application with a product<->line_item<->cart model

ruby -rubygems /home/rubys/git/rails/bin/rails new testapp
      create  README
      create  Rakefile
      create  .gitignore
      create  Gemfile
      create  app
      create  app/views/layouts/application.html.erb
      create  app/controllers/application_controller.rb
      create  app/helpers/application_helper.rb
      create  app/models
      create  config
      create  config/routes.rb
      create  config/application.rb
      create  config/environment.rb
      create  config/environments
      create  config/environments/test.rb
      create  config/environments/production.rb
      create  config/environments/development.rb
      create  config/initializers
      create  config/initializers/backtrace_silencers.rb
      create  config/initializers/inflections.rb
      create  config/initializers/session_store.rb
      create  config/initializers/mime_types.rb
      create  config/initializers/secret_token.rb
      create  config/locales
      create  config/locales/en.yml
      create  config/boot.rb
      create  config/database.yml
      create  db
      create  db/seeds.rb
      create  doc
      create  doc/README_FOR_APP
      create  lib
      create  lib/tasks
      create  lib/tasks/.gitkeep
      create  log
      create  log/server.log
      create  log/production.log
      create  log/development.log
      create  log/test.log
      create  public
      create  public/404.html
      create  public/index.html
      create  public/favicon.ico
      create  public/422.html
      create  public/500.html
      create  public/robots.txt
      create  public/images
      create  public/images/rails.png
      create  public/stylesheets
      create  public/stylesheets/.gitkeep
      create  public/javascripts
      create  public/javascripts/prototype.js
      create  public/javascripts/controls.js
      create  public/javascripts/rails.js
      create  public/javascripts/application.js
      create  public/javascripts/effects.js
      create  public/javascripts/dragdrop.js
      create  script
      create  script/rails
      create  test
      create  test/performance/browsing_test.rb
      create  test/test_helper.rb
      create  test/fixtures
      create  test/unit
      create  test/functional
      create  test/integration
      create  tmp
      create  tmp/sessions
      create  tmp/sockets
      create  tmp/cache
      create  tmp/pids
      create  vendor/plugins
      create  vendor/plugins/.gitkeep
bundle install
Using rake (0.8.7) from bundler gems 
Using abstract (1.0.0) from bundler gems 
Using activesupport (3.0.0.beta4) from source code at /home/rubys/git/rails 
Using builder (2.1.2) from bundler gems 
Using i18n (0.4.1) from bundler gems 
Using activemodel (3.0.0.beta4) from source code at /home/rubys/git/rails 
Using erubis (2.6.5) from bundler gems 
Using rack (1.1.0) from bundler gems 
Using rack-mount (0.6.3) from bundler gems 
Using rack-test (0.5.4) from bundler gems 
Using tzinfo (0.3.22) from bundler gems 
Using actionpack (3.0.0.beta4) from source code at /home/rubys/git/rails 
Using mime-types (1.16) from bundler gems 
Using polyglot (0.3.1) from bundler gems 
Using treetop (1.4.8) from bundler gems 
Using mail (2.2.3) from bundler gems 
Using actionmailer (3.0.0.beta4) from source code at /home/rubys/git/rails 
Using arel (0.4.0) from bundler gems 
Using activerecord (3.0.0.beta4) from source code at /home/rubys/git/rails 
Using activeresource (3.0.0.beta4) from source code at /home/rubys/git/rails 
Using bundler (0.9.26) from bundler gems 
Using thor (0.13.6) from bundler gems 
Using railties (3.0.0.beta4) from source code at /home/rubys/git/rails 
Using rails (3.0.0.beta4) from source code at /home/rubys/git/rails 
Using sqlite3-ruby (1.3.0) from bundler gems 
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.
rails generate scaffold product
      invoke  active_record
      create    db/migrate/20100611145844_create_products.rb
      create    app/models/product.rb
      invoke    test_unit
      create      test/unit/product_test.rb
      create      test/fixtures/products.yml
       route  resources :products
      invoke  scaffold_controller
      create    app/controllers/products_controller.rb
      invoke    erb
      create      app/views/products
      create      app/views/products/index.html.erb
      create      app/views/products/edit.html.erb
      create      app/views/products/show.html.erb
      create      app/views/products/new.html.erb
      create      app/views/products/_form.html.erb
      invoke    test_unit
      create      test/functional/products_controller_test.rb
      invoke    helper
      create      app/helpers/products_helper.rb
      invoke      test_unit
      create        test/unit/helpers/products_helper_test.rb
      invoke  stylesheets
      create    public/stylesheets/scaffold.css
rails generate scaffold cart
      invoke  active_record
      create    db/migrate/20100611145846_create_carts.rb
      create    app/models/cart.rb
      invoke    test_unit
      create      test/unit/cart_test.rb
      create      test/fixtures/carts.yml
       route  resources :carts
      invoke  scaffold_controller
      create    app/controllers/carts_controller.rb
      invoke    erb
      create      app/views/carts
      create      app/views/carts/index.html.erb
      create      app/views/carts/edit.html.erb
      create      app/views/carts/show.html.erb
      create      app/views/carts/new.html.erb
      create      app/views/carts/_form.html.erb
      invoke    test_unit
      create      test/functional/carts_controller_test.rb
      invoke    helper
      create      app/helpers/carts_helper.rb
      invoke      test_unit
      create        test/unit/helpers/carts_helper_test.rb
      invoke  stylesheets
   identical    public/stylesheets/scaffold.css
rails generate scaffold line_item count:integer cart_id:integer product_id:integer
      invoke  active_record
      create    db/migrate/20100611145847_create_line_items.rb
      create    app/models/line_item.rb
      invoke    test_unit
      create      test/unit/line_item_test.rb
      create      test/fixtures/line_items.yml
       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/functional/line_items_controller_test.rb
      invoke    helper
      create      app/helpers/line_items_helper.rb
      invoke      test_unit
      create        test/unit/helpers/line_items_helper_test.rb
      invoke  stylesheets
   identical    public/stylesheets/scaffold.css
rake db:migrate
mv 20100611145844_create_products.rb 20100301000001_create_products.rb
mv 20100611145846_create_carts.rb 20100301000002_create_carts.rb
mv 20100611145847_create_line_items.rb 20100301000003_create_line_items.rb
(in /home/rubys/tmp/work/testapp)
==  CreateProducts: migrating =================================================
-- create_table(:products)
   -> 0.0013s
==  CreateProducts: migrated (0.0014s) ========================================
==  CreateCarts: migrating ====================================================
-- create_table(:carts)
   -> 0.0012s
==  CreateCarts: migrated (0.0013s) ===========================================
==  CreateLineItems: migrating ================================================
-- create_table(:line_items)
   -> 0.0014s
==  CreateLineItems: migrated (0.0015s) =======================================
edit app/models/cart.rb
class Cart < ActiveRecord::Base
  has_many :line_items
  def add_product(product_id)
    current_item = line_items.where(:product_id => product_id).first
    if current_item
      current_item.count += 1
      current_item =>product_id, :count=>1)
      line_items << current_item
edit app/models/product.rb
class Product < ActiveRecord::Base
  has_many :line_items
edit app/models/product.rb
class Product < ActiveRecord::Base
  has_many :line_items
  belongs_to :product
  belongs_to :cart
rake test:units
(in /home/rubys/tmp/work/testapp)
Loaded suite /home/rubys/.rvm/gems/ruby-1.8.7-p249/gems/rake-0.8.7/lib/rake/rake_test_loader
Finished in 0.095847 seconds.
3 tests, 3 assertions, 0 failures, 0 errors
edit test/unit/cart_test.rb
require 'test_helper'
class CartTest < ActiveSupport::TestCase
  # Replace this with your real tests.
  test "the truth" do
    assert true
  test "add two different products" do
    cart = Cart.create
    assert_equal 2, cart.line_items.count
    assert_equal 2, cart.line_items.sum(:count)
  test "add two of the same product" do
    cart = Cart.create
    assert_equal 1, cart.line_items.count
    assert_equal 2, cart.line_items[0].count
rake test:units
(in /home/rubys/tmp/work/testapp)
Loaded suite /home/rubys/.rvm/gems/ruby-1.8.7-p249/gems/rake-0.8.7/lib/rake/rake_test_loader
Finished in 0.204797 seconds.
  1) Failure:
test_add_two_of_the_same_product(CartTest) [/test/unit/cart_test.rb:21]:
<2> expected but was
5 tests, 7 assertions, 1 failures, 0 errors
rake aborted!
Command failed with status (1): [/home/rubys/.rvm/rubies/ruby-1.8.7-p249/bi...]

(See full trace by running task with --trace)
</5 tests, 7 assertions, 0 failures, 0 errors/> expected but was
<"(in /home/rubys/tmp/work/testapp)">.

  /home/rubys/git/gorp/lib/gorp/test.rb:148:in `call'
  /home/rubys/git/gorp/lib/gorp/test.rb:148:in `rake'


Fri, 11 Jun 2010 14:58:58 GMT
/home/rubys/.rvm/rubies/ruby-1.8.7-p249/bin/ruby -v
ruby 1.8.7 (2010-01-10 patchlevel 249) [x86_64-linux]
gem -v
bundle show
Gems included by the bundle:
  * abstract (1.0.0)
  * actionmailer (3.0.0.beta4)
  * actionpack (3.0.0.beta4)
  * activemodel (3.0.0.beta4)
  * activerecord (3.0.0.beta4)
  * activeresource (3.0.0.beta4)
  * activesupport (3.0.0.beta4)
  * arel (0.4.0)
  * builder (2.1.2)
  * bundler (0.9.26)
  * erubis (2.6.5)
  * i18n (0.4.1)
  * mail (2.2.3)
  * mime-types (1.16)
  * polyglot (0.3.1)
  * rack (1.1.0)
  * rack-mount (0.6.3)
  * rack-test (0.5.4)
  * rails (3.0.0.beta4 635606)
  * railties (3.0.0.beta4)
  * rake (0.8.7)
  * sqlite3-ruby (1.3.0)
  * thor (0.13.6)
  * treetop (1.4.8)
  * tzinfo (0.3.22)
rake about
(in /home/rubys/tmp/work/testapp)
About your application's environment
Ruby version              1.8.7 (x86_64-linux)
RubyGems version          1.3.7
Rack version              1.1
Rails version             3.0.0.beta4
Active Record version     3.0.0.beta4
Action Pack version       3.0.0.beta4
Active Resource version   3.0.0.beta4
Action Mailer version     3.0.0.beta4
Active Support version    3.0.0.beta4
Application root          /home/rubys/tmp/work/testapp
Environment               development
git log -1
commit 63560660062d552d6bbebec007154f0c639bf865
Merge: 59e89fa 61fc7a4
Author: Xavier Noria <>
Date:   Thu Jun 10 22:00:55 2010 +0200

    Merge remote branch 'rails/master'