Skip to content

Instantly share code, notes, and snippets.

@samwgoldman
Created February 18, 2012 09:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samwgoldman/1858435 to your computer and use it in GitHub Desktop.
Save samwgoldman/1858435 to your computer and use it in GitHub Desktop.
activerecord bug?
active record
role built from a context
context
should eq #<Context id: 1, type: nil>
role built from a role through context
context
should eq #<Context id: 2, type: nil> (FAILED - 1)
student built from a program
program
should eq #<Program id: 3, type: "Program">
student built from a teacher through a program
program
should eq #<Program id: 4, type: "Program"> (FAILED - 2)
Failures:
1) active record role built from a role through context context
Failure/Error: its(:context) { should eq(context) }
expected: #<Context id: 2, type: nil>
got: nil
(compared using ==)
# ./spec/build_has_many_through_association_spec.rb:63:in `block (3 levels) in <top (required)>'
2) active record student built from a teacher through a program program
Failure/Error: its(:program) { should eq(program) }
expected: #<Program id: 4, type: "Program">
got: nil
(compared using ==)
# ./spec/build_has_many_through_association_spec.rb:75:in `block (3 levels) in <top (required)>'
Finished in 0.23493 seconds
4 examples, 2 failures
Failed examples:
rspec ./spec/build_has_many_through_association_spec.rb:63 # active record role built from a role through context context
rspec ./spec/build_has_many_through_association_spec.rb:75 # active record student built from a teacher through a program program
source "https://rubygems.org"
gem "rails", :git => "https://github.com/rails/rails"
gem "sqlite3"
gem "rspec"
active record
role built from a context
context
should eq #<Context id: 1, type: nil>
role built from a role through context
context
should eq #<Context id: 2, type: nil> (FAILED - 1)
student built from a program
program
should eq #<Program id: 3, type: "Program">
student built from a teacher through a program
program
should eq #<Program id: 4, type: "Program"> (FAILED - 2)
Failures:
1) active record role built from a role through context context
Failure/Error: its(:context) { should eq(context) }
expected: #<Context id: 2, type: nil>
got: nil
(compared using ==)
# ./spec/build_has_many_through_association_spec.rb:63:in `block (3 levels) in <top (required)>'
2) active record student built from a teacher through a program program
Failure/Error: its(:program) { should eq(program) }
expected: #<Program id: 4, type: "Program">
got: nil
(compared using ==)
# ./spec/build_has_many_through_association_spec.rb:75:in `block (3 levels) in <top (required)>'
Finished in 0.23493 seconds
4 examples, 2 failures
Failed examples:
rspec ./spec/build_has_many_through_association_spec.rb:63 # active record role built from a role through context context
rspec ./spec/build_has_many_through_association_spec.rb:75 # active record student built from a teacher through a program program
require 'bundler'
Bundler.setup
require 'active_record'
class Migrate < ActiveRecord::Migration
self.verbose = false
def up
create_table :contexts do |t| t.string :type; end
create_table :roles do |t| t.string :type; t.references :context; end
end
end
class Context < ActiveRecord::Base
has_many :roles, :inverse_of => :context
end
class Role < ActiveRecord::Base
belongs_to :context, :inverse_of => :roles
has_many :roles, :through => :context
end
class Program < Context
has_many :teachers, :foreign_key => "context_id", :inverse_of => :program
has_many :students, :foreign_key => "context_id", :inverse_of => :program
end
class Teacher < Role
belongs_to :program, :foreign_key => "context_id", :inverse_of => :teachers
has_many :students, :through => :program
end
class Student < Role
belongs_to :program, :foreign_key => "context_id", :inverse_of => :students
has_many :teachers, :through => :program
end
describe "active record" do
before(:all) do
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => "db")
Migrate.new.up
end
after(:all) do
File.unlink("db")
end
let(:context) { Context.create }
let(:role) { context.roles.create }
let(:program) { Program.create }
let(:teacher) { program.teacher.create }
describe "role built from a context" do
subject { context.roles.build }
its(:context) { should eq(context) }
end
describe "role built from a role through context" do
subject { role.roles.build }
its(:context) { should eq(context) }
end
context "student built from a program" do
subject { program.students.build }
its(:program) { should eq(program) }
end
context "student built from a teacher through a program" do
subject { teacher.students.build }
its(:program) { should eq(program) }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment