He tenido problemas para crear fábricas para algunos objetos y asociaciones que he definido en mi proyecto. Tengo un tipo de asociación cíclica, donde un objeto se asocia con otros dos objetos que luego se unen.¿Cómo podría crear fábricas para una asociación cíclica en FactoryGirl?
+--------------+ +-------------+
| | | |
| TestCase +---------> | TestDataGrid|
| | | |
+------+-------+ +------+------+
| |
| |
| |
v v
+--------------+ +--------------+
| | | |
| | | |
| TestVariable | | TestDataSet |
| | | |
+------+-------+ +------+-------+
| |
| |
| |
| |
| +---------------+ |
| | | |
| | | |
+---> | TestDataValue |<---+
| |
+---------------+
class TestCase < ActiveRecord::Base
has_many :test_variables, dependent: :destroy
has_many :test_data_grids
#...omitted code...
end
class TestVariable < ActiveRecord::Base
belongs_to :test_case
has_many :test_data_values
#...omitted code...
end
class TestDataValue < ActiveRecord::Base
belongs_to :test_variable
belongs_to :test_data_set
#...omitted code...
end
class TestDataSet < ActiveRecord::Base
belongs_to :test_data_grid
has_many :test_data_values
#...omitted code...
end
class TestDataGrid < ActiveRecord::Base
belongs_to :test_case
has_many :test_data_sets
#...omitted code...
end
Básicamente la asociación se divide en TestCase y se une de nuevo en TestDataValue, ¿cómo podría crear una fábrica que se abre y se cierra el círculo con los mismos objetos?
¿Realmente lo necesita? En la mayoría de los casos, puedes burlarte de todas esas relaciones. Es extremadamente difícil mantener fábricas tan complejas. – Sigurd