views:

101

answers:

0

class Addressee < AR::Base end

class Employee < Addressee
  belongs_to :person
  belongs_to :company
end

class Person < Addressee
  has_many :employees
  has_many :companies, :through => :employees
end

class Company < Addressee
  has_many :employees
  has_many :people, :through => :employees
end

Given the models above, when I run the following in my tests:

class EmployeeAddresseeTest < ActiveSupport::TestCase

  context "Given an employee" do

    setup do
      @employee = Employee.create!(
                    :person => Person.create!(:first_name => "Francois"),
                    :company => Company.create!(:company_name => "STI"))
    end

    should "find the company from the person through the employee" do
      assert_equal [@employee.company], @employee.person.companies
    end

    should "find the person from the company through the person" do
      assert_equal [@employee.person], @employee.company.people
    end

  end

end

I end up with the following errors in my test:

  1) Error:
test: Given an employee should find the company from the person through the employee. (EmployeeAddresseeTest):
ActiveRecord::StatementInvalid: PGError: ERROR:  table name "addressees" specified more than once
: SELECT "addressees".* FROM "addressees"  INNER JOIN "addressees" ON "addressees".id = "addressees".company_id    WHERE (("addressees".person_id = 60)) 
    vendor/plugins/hobo/hobo/lib/hobo/model.rb:280:in `find_by_sql'
    vendor/plugins/hobo/hobo/lib/hobo/model.rb:273:in `find'
    /Users/francois/.rvm/gems/ruby-1.8.7-p249/gems/will_paginate-2.3.14/lib/will_paginate/finder.rb:170:in `method_missing'
    test/unit/employee_addressee_test.rb:28:in `__bind_1276106423_69152'
    shoulda (2.10.3) lib/shoulda/context.rb:362:in `call'
    shoulda (2.10.3) lib/shoulda/context.rb:362:in `test: Given an employee should find the company from the person through the employee. '

  2) Error:
test: Given an employee should find the person from the company through the person. (EmployeeAddresseeTest):
ActiveRecord::StatementInvalid: PGError: ERROR:  table name "addressees" specified more than once
: SELECT "addressees".* FROM "addressees"  INNER JOIN "addressees" ON "addressees".id = "addressees".person_id    WHERE (("addressees".company_id = 64)) 
    vendor/plugins/hobo/hobo/lib/hobo/model.rb:280:in `find_by_sql'
    vendor/plugins/hobo/hobo/lib/hobo/model.rb:273:in `find'
    /Users/francois/.rvm/gems/ruby-1.8.7-p249/gems/will_paginate-2.3.14/lib/will_paginate/finder.rb:170:in `method_missing'
    test/unit/employee_addressee_test.rb:32:in `__bind_1276106423_91232'
    shoulda (2.10.3) lib/shoulda/context.rb:362:in `call'
    shoulda (2.10.3) lib/shoulda/context.rb:362:in `test: Given an employee should find the person from the company through the person. '

I understand what the error means. I'm just wondering how to go around it. I'm using Rails 2.3.8, Ruby 1.8.7, Hobo stable on PostgreSQL 8.3. Thoughts?