# Fixtures and Legacy Tables

**URL:** <https://www.ruby-forum.com/t/fixtures-and-legacy-tables/146082>\
**Category:** Rails\
**Created:** [August 17, 2008, 4:23pm UTC](https://www.ruby-forum.com/t/fixtures-and-legacy-tables/146082 "2008-08-17T16:23:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_Menard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a3d4f5/32.png) [@Mark\_Menard](https://www.ruby-forum.com/u/Mark_Menard)\
**Post date:** [August 17, 2008, 4:23pm UTC](https://www.ruby-forum.com/t/fixtures-and-legacy-tables/146082/1 "2008-08-17T16:23:08Z")

</div>

I’m trying to use an existing database for a new Rails project.

In my tests the fixture accessors are not working. If I try to access  
a Party fixture with “parties(:one)” I get the following error:

test\_fixture(PartyTest):  
NoMethodError: You have a nil object when you didn’t expect it!  
You might have expected an instance of ActiveRecord::Base.  
The error occurred while evaluating nil.[]  
/Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active\_record/  
fixtures.rb:887:in `parties' /Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/ fixtures.rb:884:in`map’  
/Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active\_record/  
fixtures.rb:884:in `parties' ./test/unit/party_test.rb:10:in`test\_fixture’  
/Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/active\_support/  
testing/setup\_and\_teardown.rb:33:in ` __send__' /Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/active_support/ testing/setup_and_teardown.rb:33:in`run’

I have added the following to test\_helper, without which it gets the  
table name wrong:

set\_fixture\_class :parties =\> Party

My model is as follows:

class Party \< ActiveRecord::Base  
set\_table\_name :party  
end

After some debugging it appears to me that the  
@loaded\_fixtures[table\_name] is nil because table\_name == ‘parties’  
and not ‘party’. It appears that the overridden table name is not  
carrying into the setup\_fixture\_accessors method.

Has anyone else had success using legacy tables that require a  
set\_table\_name with fixtures in Rails 2.1?

Thanks,

Mark

---

<div class="post-metadata">

**Author:** ![Mark\_Menard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a3d4f5/32.png) [@Mark\_Menard](https://www.ruby-forum.com/u/Mark_Menard)\
**Post date:** [August 17, 2008, 4:37pm UTC](https://www.ruby-forum.com/t/fixtures-and-legacy-tables/146082/2 "2008-08-17T16:37:11Z")

</div>

On 17 Aug 2008, at 15:04, Mark Menard wrote:

> I’m trying to use an existing database for a new Rails project.
> 
> In my tests the fixture accessors are not working. If I try to access  
> a Party fixture with “parties(:one)” I get the following error:

Looks like you can avoid some pain if you call your fixtures file  
party.yml and use party(:one) instead of parties(:one)

Fred

---

<div class="post-metadata">

**Author:** ![Mark\_Menard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a3d4f5/32.png) [@Mark\_Menard](https://www.ruby-forum.com/u/Mark_Menard)\
**Post date:** [August 17, 2008, 5:18pm UTC](https://www.ruby-forum.com/t/fixtures-and-legacy-tables/146082/3 "2008-08-17T17:18:20Z")

</div>

Thanks Fred,

That worked. I’ve gone through so many iterations that I thought I’d  
tried that setup already.

Mark

On Aug 17, 10:36 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
