I’m trying to set up some new unit tests for classes I’m adding to a project. Since I’m on Rails 2.1, I figured I’d make use of the Foxy Fixtures to make things oh so much easier. Except that’s not the way my day has turned out.
I’ve got three classes where the parent class has a list of children, which in turn has a list of grandchildren:
parent:
id: integer
name: text
has_many :children, :order => :position
child:
id: integer
parent_id: integer
name: text
position: integer
has_many :grandchildren, :order => :position
belongs_to :parent
acts_as_list :scope => :parent
grandchild:
id: integer
child_id: integer
name: text
position: integer
belongs_to: child
acts_as_list :scope => :child
(These aren’t the real class names, I’m just using them to make my explanation a bit clearer.)
When I created my fixtures, I made several parent objects without using any erb:
parent_1:
name: Some Parent
parent_2:
name: Some Parent
This resulted in DB entries (MySQL) like this:
id name created_at updated_at
461483557 Some Parent 2009-02-17 01:13:54 2009-02-17 01:13:54
461483556 Some Parent 2009-02-17 01:13:54 2009-02-17 01:13:54
The ids were hashed from the labels of the YAML fixture as expected.
Clearly I’m going to have some massive growth in the number of entries I need for the children and grandchildren fixtures, so I wanted to use ERB to help keep things clean and simple. For the child.yml file, I wanted to have code that looks like this, but I ran into two problems:
<%
parents = [ "parent_1", "parent_2" ]
%>
<% parents.each do |parent| %>
<%= parent %>_child1:
position: 1
name: Kid One
parent: <%= parent %>
<%= parent %>_child2:
position: 2
name: Kid Two
parent: <%= parent %>
<%= parent %>_child3:
position: 3
name: Kid Three
parent: <%= parent %>
<% end %>
First, the database reports this error:
Mysql::Error: Unknown column ‘parent’ in ‘field list’: INSERT INTO children
(name
, parent
, position
) VALUES (‘Kid One’, ‘parent_1’, 1)
I did find a workaround for this problem, but I don’t like it and I’d much prefer being able to simply use the name of the parent object in the parent.yml file. Here’s the workaround I used:
parent_id: <%= parent.hash.abs %>
Which led me to a second problem in the IDs that are generated for the child objects. They’re simply sequential numbers, like this:
id parent_id name created_at updated_at
1 461483557 Kid One 2009-02-17 01:13:54 2009-02-17 01:13:54
2 461483557 Kid Two 2009-02-17 01:13:54 2009-02-17 01:13:54
3 461483557 Kid Three 2009-02-17 01:13:54 2009-02-17 01:13:54
I was expecting the id column to contain hashed values from “parent1_child1”, “parent1_child2”, “parent1_child3” and so on, but since they’re not, I can’t use my workaround when I create the fixtures in the grandchildren.yml file.
Ideally, I’d like to be able to specify the belongs_to fixture object by its label in the .yml file and not use my workaround at all. Though it does concern me that sometimes fixtures seem to use the hashed value of the label as an ID and sometimes just use a sequence unrelated to the label.
If anyone could give me some idea of where I should look for the problem, I’d really appreciate it.
Thank you,
Mark