# Rails 2.0 Fixtures bug?

**URL:** <https://www.ruby-forum.com/t/rails-2-0-fixtures-bug/126035>\
**Category:** Rails\
**Created:** [January 14, 2008, 10:46pm UTC](https://www.ruby-forum.com/t/rails-2-0-fixtures-bug/126035 "2008-01-14T22:46:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![fifoo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/82dd89/32.png) [@fifoo](https://www.ruby-forum.com/u/fifoo)\
**Post date:** [January 14, 2008, 10:46pm UTC](https://www.ruby-forum.com/t/rails-2-0-fixtures-bug/126035/1 "2008-01-14T22:46:52Z")

</div>

All,

I’ve started playing with Rails 2.0.2 and I have an issue with fixtures.  
It used to work in Rails 1.8.6…

A bit of context first. I have 2 models:

class competition  
has\_and\_belongs\_to\_many :teams  
end

class team  
has\_and\_belongs\_to\_many :competitions  
end

In my competition\_test.rb, I have:

def test\_teams\_association  
assert competitions(:competition\_one).teams.include? teams(:team\_one)  
end

First I know that this is testing ActiveRecord more than anything else  
but still this test used to pass in Rails 1.8.6 but fails with Rails  
2.0.

I printed out competitions(:competition\_one).teams.last (if we imagine  
that :team\_one is indeed the last in the array) and teams(:team\_one) and  
I get something like:

competitions(:competition\_one).teams.last  
=\> #\<Team id: 487, name: “Team One”, created\_at: nil, updated\_at: nil\>

teams(:team\_one)  
=\> #\<Team id: 607411122, name: “Team One”, created\_at: “2008-01-14  
21:26:05”, updated\_at: “2008-01-14 21:26:05”\>

Has anyone come with this issue and know what it’s all about?

Thanks for your help.

---

<div class="post-metadata">

**Author:** ![fifoo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/82dd89/32.png) [@fifoo](https://www.ruby-forum.com/u/fifoo)\
**Post date:** [January 14, 2008, 11:35pm UTC](https://www.ruby-forum.com/t/rails-2-0-fixtures-bug/126035/2 "2008-01-14T23:35:42Z")

</div>

On Jan 14, 2008 1:46 PM, Laurent Colloud  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’ve started playing with Rails 2.0.2 and I have an issue with fixtures.  
> It used to work in Rails 1.8.6…

Hi Laurent,

Can you show us a link to a pastie of your fixture files? That’ll help  
show what’s going on.

~ j.

---

<div class="post-metadata">

**Author:** ![fifoo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/82dd89/32.png) [@fifoo](https://www.ruby-forum.com/u/fifoo)\
**Post date:** [January 15, 2008, 9:29am UTC](https://www.ruby-forum.com/t/rails-2-0-fixtures-bug/126035/3 "2008-01-15T09:29:51Z")

</div>

Hi John,

Thanks for your reply. Here are the fixture files:

## competitions.yml

competition\_one:  
name: Competition One  
teams: team\_one, team\_two

## teams.yml

team\_one:  
name: Team One

team\_two:  
name: Team Two

---

<div class="post-metadata">

**Author:** ![fifoo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/82dd89/32.png) [@fifoo](https://www.ruby-forum.com/u/fifoo)\
**Post date:** [January 15, 2008, 7:12pm UTC](https://www.ruby-forum.com/t/rails-2-0-fixtures-bug/126035/4 "2008-01-15T19:12:58Z")

</div>

On Jan 15, 2008 12:29 AM, Laurent Colloud  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks for your reply. Here are the fixture files:

Based on these fixture files and your output above, it looks like you  
might have an unclean test database.

> competitions(:competition\_one).teams.last  
> =\> #\<Team id: 487, name: “Team One”, created\_at: nil, updated\_at: nil\>

> teams(:team\_one)  
> =\> #\<Team id: 607411122, name: “Team One”, created\_at: “2008-01-14  
> 21:26:05”, updated\_at: “2008-01-14 21:26:05”\>

I bet you have two teams named “Team One”: One that was inserted into  
the database before your upgrade to Rails 2 (ID 487), and one after  
(ID 607411122). Try dropping your databases and rerunning.

~ j.

---

<div class="post-metadata">

**Author:** ![fifoo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/82dd89/32.png) [@fifoo](https://www.ruby-forum.com/u/fifoo)\
**Post date:** [January 16, 2008, 2:28am UTC](https://www.ruby-forum.com/t/rails-2-0-fixtures-bug/126035/5 "2008-01-16T02:28:19Z")

</div>

Laurent Colloud wrote:  
teams table!

> I dropped it, recreated it by hand with only competition\_id and team\_id  
> columns and it is now all working fine :).
> 
> I suppose you always have to create the link tables by hand? Or is there  
> a Rails way for this too?

You might find it useful to take a look at the FixtureReplacement plugin  
at:

[http://replacefixtures.rubyforge.org/](http://replacefixtures.rubyforge.org/)

I just discovered it today so I cannot speak from my own experience, but  
it came highly recommended.

---

<div class="post-metadata">

**Author:** ![fifoo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/82dd89/32.png) [@fifoo](https://www.ruby-forum.com/u/fifoo)\
**Post date:** [January 15, 2008, 10:17pm UTC](https://www.ruby-forum.com/t/rails-2-0-fixtures-bug/126035/6 "2008-01-15T22:17:56Z")

</div>

John,

Thanks for pointing me towards the right direction. It now works.

I first dropped all databases and started again with no success. I  
actually took a look at the test database, namely the  
competitions\_teams table.

I created it using a migration file like :

def self.up  
create\_table :competitions\_teams do |t|  
t.references :competition, :team  
t.timestamps  
end  
end

and the table had the following columns:

| id | competition\_id | team\_id | updated\_at | created\_at |

In the test I re-ran, I now had “26” as the id for the  
competitions(:competition\_one).teams.last which turned out to be one of  
the primary key of my competitions\_teams table!

I dropped it, recreated it by hand with only competition\_id and team\_id  
columns and it is now all working fine :).

I suppose you always have to create the link tables by hand? Or is there  
a Rails way for this too?

Thanks again for your help.
