Skip to content

Simple Rails 3.2 App built to show a bug in when ActiveRecord creates new instances of models backed by views

Notifications You must be signed in to change notification settings

stevenchanin/view_test

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

View Test

Simple rails 3.2 app designed to demonstrate strange behavior when ActiveRecord creates an instance of a model backed by a view.

This app contains two models:

  • Cars

  • Manufacturers

    class Car < ActiveRecord::Base
      belongs_to :manufacturer
    
      attr_accessible :name, :manufacturer
    end
    
    class Manufacturer < ActiveRecord::Base
      attr_accessible :name
    
      self.table_name = 'v_manufacturers'
      self.primary_key = 'id'
    end
    

Manufacturers link to a view on top of a “legacy table” with non-conventional column names. Cars is a normal Rails convention table

When a new manufacturer is created, the id defaults to 0 rather than nil:

class ManufacturerTest < ActiveSupport::TestCase
  test "a new manufacturer should have id = nil" do
    assert Manufacturer.new(name: 'Toyota').id.nil?
  end
end

However, when a car is created, the id defaults to nil (the correct behavior)

class CarTest < ActiveSupport::TestCase
  test "a new car should have id = nil" do
    toyota = Manufacturer.create(name: 'Toyota')

    supra = Car.new(name: 'Supra', manufacturer: toyota)
    assert supra.id.nil?
  end
end

this can also be verified by running

rake db:create db:migrate db:test:prepare
rake test

NOTE: this assumes you have mysql setup with the root account having a blank password. If not, please edit database.yml

About

Simple Rails 3.2 App built to show a bug in when ActiveRecord creates new instances of models backed by views

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published