Rails 2.0 Fixtures bug?

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.

Hi Laurent,

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

~ j.

Hi John,

Thanks for your reply. Here are the fixture files:

competitions.yml

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.

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.

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/

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