Is there a way to load Rails fixtures in a specific order while running tests? For example, take the following classes...
class User < ActiveRecord::Base
has_many :memberships
has_many :groups, through: :memberships
end
class Group < ActiveRecord::Base
has_many :memberships
has_many :users, through: :memberships
end
class Membership < ActiveRecord::Base
belongs_to :user
belongs_to :group
end
Memberships
have a database level foreign key constraint requiring Users
and Groups
to be present before they can be created. However, because Rails loads fixture alphabetically, Memberships
are loaded before Users
, and an error occurs stating the relationships do not exist (rightly so).
ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation: ERROR: insert or update on table "memberships" violates foreign key constraint
Is there a way to load the Users
and Groups
fixtures before loading Memberships
while running tests?
FIXTURE
variable.