Hi, Is there a workaround for using fixtures on tables that don''t have a single primary key? The table setup: - movies (id, title, year) - artists (id, name, birthdate) - credits (movie_id, artist_id, role) A credits fixture looks like this: matrix_keanu: movie_id: 1 artist_id: 1 role: 2 Upon unit testing, I get "ActiveRecord::RecordNotFound: Couldn''t find Credit without an ID" I traced it back to self.instantiate_fixtures in fixtures.rb, line 203 (Rails 0.13.1, ActiveRecord 1.11.1) I can use the credits table fine elsewhere in the application, but fixtures seem to rely on a single primary key. Just for completeness, here are my classes: class Movie < ActiveRecord::Base has_many :credits end class Artist < ActiveRecord::Base has_many :credits end class Credit < ActiveRecord::Base belongs_to :artist belongs_to :movie end Any suggestions? Frederik
On 10/4/05, Frederik De Bleser <frederik-LPO8gxj9N8aZIoH1IeqzKA@public.gmane.org> wrote:> Hi, > > Is there a workaround for using fixtures on tables that don''t have a > single primary key? > > The table setup: > > - movies (id, title, year) > - artists (id, name, birthdate) > - credits (movie_id, artist_id, role) > > A credits fixture looks like this: > > matrix_keanu: > movie_id: 1 > artist_id: 1 > role: 2 > > Upon unit testing, I get "ActiveRecord::RecordNotFound: Couldn''t find > Credit without an ID" > > I traced it back to self.instantiate_fixtures in fixtures.rb, line > 203 (Rails 0.13.1, ActiveRecord 1.11.1) > > I can use the credits table fine elsewhere in the application, but > fixtures seem to rely on a single primary key. > > Just for completeness, here are my classes: > > class Movie < ActiveRecord::Base > has_many :credits > end > > class Artist < ActiveRecord::Base > has_many :credits > end > > class Credit < ActiveRecord::Base > belongs_to :artist > belongs_to :movie > end > > Any suggestions? > > Frederik > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >You would be best off to add a surrogate key to the credits table. It will make many parts of your rails manipulations much easier. Normalizaiton is beautiful, but some denormalization is efficient for the developer. Happy coding! Peter Fitzgibbons
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Oct 4, 2005, at 4:55 AM, Peter Fitzgibbons wrote:> On 10/4/05, Frederik De Bleser <frederik-LPO8gxj9N8aZIoH1IeqzKA@public.gmane.org> wrote: >> Is there a workaround for using fixtures on tables that don''t have a >> single primary key? >> [...] >> I traced it back to self.instantiate_fixtures in fixtures.rb, line >> 203 (Rails 0.13.1, ActiveRecord 1.11.1)> You would be best off to add a surrogate key to the credits table. It > will make many parts of your rails manipulations much easier. > Normalizaiton is beautiful, but some denormalization is efficient for > the developer.I agree with Peter, but you can all turn off instantiated fixtures. They''re there as a convenience for your tests, but mean a SELECT per- fixture, per-test (read: they slow down your tests.) class FooTest < Test::Unit::TestCase self.use_instantiate_fixtures = false end or globally, in test/test_helper.rb Test::Unit::TestCase.use_instantiated_fixtures = false Best, jeremy -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2 (Darwin) iD8DBQFDQqhGAQHALep9HFYRAixhAJ9BR9GNmXF38fiWixbfO/KpIhX8SACgqSnj NHmt2QXiNW6ySD8G4h+6cPk=mZc7 -----END PGP SIGNATURE-----