# Foxy Fixtures making inconsistent ids

**URL:** <https://www.ruby-forum.com/t/foxy-fixtures-making-inconsistent-ids/160520>\
**Category:** Rails\
**Created:** [February 17, 2009, 10:54am UTC](https://www.ruby-forum.com/t/foxy-fixtures-making-inconsistent-ids/160520 "2009-02-17T10:54:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mslagell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mslagell](https://www.ruby-forum.com/u/mslagell)\
**Post date:** [February 17, 2009, 10:54am UTC](https://www.ruby-forum.com/t/foxy-fixtures-making-inconsistent-ids/160520/1 "2009-02-17T10:54:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mslagell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mslagell](https://www.ruby-forum.com/u/mslagell)\
**Post date:** [February 17, 2009, 1:11pm UTC](https://www.ruby-forum.com/t/foxy-fixtures-making-inconsistent-ids/160520/2 "2009-02-17T13:11:01Z")

</div>

it just tried it in a new rails project. foxy fixtures are working  
fine for me. neither of your problems occurr:

- ids are hashed values.
- relationships in yml-files work

here is my fixture-code:

# products.yml

first:  
name: first

second:  
name: second

# pictures.yml

one:  
product: second

two:  
product: first

maybe you got some other problems in your real code ( the one you  
posted looked quite ok).

---

<div class="post-metadata">

**Author:** ![mslagell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mslagell](https://www.ruby-forum.com/u/mslagell)\
**Post date:** [February 18, 2009, 12:47am UTC](https://www.ruby-forum.com/t/foxy-fixtures-making-inconsistent-ids/160520/3 "2009-02-18T00:47:28Z")

</div>

On Feb 17, 1:00 pm, markds75 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Okay, well here’s the real code I’m dealing with (sorry if its long).  
> I’m adding surveys to a research project I’ve been working on, So I’ve  
> got three migration files, one to create the surveys, one to create  
> the survey questions, and another to create the possible survey  
> answers.  
> [snip]  
> class SurveyQuestions \< ActiveRecord::Base  
> acts\_as\_list :scope =\> :survey\_questions  
> end

The problem: the belongs\_to here is going to fail. In the vast  
majority of cases,  
belongs\_to should have a singular name. The code here won’t ever work,  
as you’ve  
got the DB field (survey\_question\_id) named correctly, and it won’t  
match.

BTW, tiny typos like this are why I prefer to start by poking at the  
models in console;  
frequently, that will drive the first tests of the models, and if  
you’ve got a typo it will  
be very apparent…

–Matt J.

---

<div class="post-metadata">

**Author:** ![mslagell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mslagell](https://www.ruby-forum.com/u/mslagell)\
**Post date:** [February 17, 2009, 7:01pm UTC](https://www.ruby-forum.com/t/foxy-fixtures-making-inconsistent-ids/160520/4 "2009-02-17T19:01:37Z")

</div>

Okay, well here’s the real code I’m dealing with (sorry if its long).  
I’m adding surveys to a research project I’ve been working on, So I’ve  
got three migration files, one to create the surveys, one to create  
the survey questions, and another to create the possible survey  
answers.

###\_create\_surveys.rb:

class CreateSurveys \< ActiveRecord::Migration  
def self.up  
create\_table :surveys do |t|  
t.boolean :required  
t.string :title  
t.datetime :go\_live

```
  t.timestamps
end

```

end

def self.down  
drop\_table :surveys  
end  
end

###\_create\_survey\_answers.rb:

class CreateSurveyQuestions \< ActiveRecord::Migration  
def self.up  
create\_table :survey\_questions do |t|  
t.integer :position  
t.text :question  
t.integer :survey\_id  
t.string :answer\_style

```
  t.timestamps
end

```

end

def self.down  
drop\_table :survey\_questions  
end  
end

###\_create\_survey\_possible\_answers:

class CreateSurveyPossibleAnswers \< ActiveRecord::Migration  
def self.up  
create\_table :survey\_possible\_answers do |t|  
t.integer :position  
t.text :answer  
t.integer :survey\_question\_id

```
  t.timestamps
end

```

end

def self.down  
drop\_table :survey\_possible\_answers  
end  
end

And I’ve got 3 models to go with the new classes.

survey.rb:

class Survey \< ActiveRecord::Base  
has\_many :survey\_questions, :order =\> :position  
end

survey\_questions.rb:

class SurveyQuestions \< ActiveRecord::Base  
belongs\_to :survey  
acts\_as\_list :scope =\> :survey

has\_many :survey\_possible\_answers  
end

survey\_possible\_answers.rb:

class SurveyPossibleAnswers \< ActiveRecord::Base  
belongs\_to :survey\_questions, :order =\> :position  
acts\_as\_list :scope =\> :survey\_questions  
end

And finally, here’s the test data I was trying to use.

surveys.yml:

initial\_survey:  
required: true  
title: Initial Survey  
go\_live: 2009-01-01 00:00:00

past\_weekly\_1:  
required: false  
title: Past Weekly One  
go\_live: 2009-02-01 00:00:00

past\_weekly\_2:  
required: false  
title: Past Weekly Two  
go\_live: 2009-02-01 00:00:00

future\_weekly\_1:  
required: false  
title: Future Weekly One  
go\_live: \<%= Date.today.next.strftime( “%Y-%m-%d” ) %\>

future\_requred\_1:  
required: true  
title: Future Required  
go\_live: \<%= Date.today.next.strftime( “%Y-%m-%d” ) %\>

survey\_questions.yml:

# Read about fixtures at [http://ar.rubyonrails.org/classes/Fixtures.html](http://ar.rubyonrails.org/classes/Fixtures.html)

\<%  
surveys = [ “initial\_survey”, “past\_weekly\_1”, “past\_weekly\_2”,  
“future\_weekly\_1”, “future\_requred\_1” ]  
%\>

\<% surveys.each do |survey| %\>

\<%= survey %\>\_q1:  
position: 1  
question: My Question?  
survey: \<%= survey %\>

\<%= survey %\>\_q2:  
position: 2  
question: My Question?  
survey: \<%= survey %\>

\<%= survey %\>\_q3:  
position: 3  
question: My Question?  
survey: \<%= survey %\>

\<% end %\>

survey\_possible\_answers.yml:

\<%  
surveys = [ “initial\_survey”, “past\_weekly\_1”, “past\_weekly\_2”,  
“future\_weekly\_1”, “future\_requred\_1” ]  
numQuestions = 3  
%\>

\<% surveys.each do |survey| %\>  
\<% for i in (1…numQuestions) %\>

\<%= survey %\>\_q\<%= i %\>\_a1:  
position: 1  
answer: Some Answer  
survey\_question: \<%= survey %\>\_q\<%= i %\>

\<%= survey %\>\_q\<%= i %\>\_a2:  
position: 2  
answer: Some Answer  
survey\_question: \<%= survey %\>\_q\<%= i %\>

\<%= survey %\>\_q\<%= i %\>\_a3:  
position: 3  
answer: Some Answer  
survey\_question: \<%= survey %\>\_q\<%= i %\>  
\<% end %\>  
\<% end %\>

So … after all that, I get errors like this when I try to run rake  
test:units:

ActiveRecord::StatementInvalid: Mysql::Error: Unknown column  
‘survey\_question’ in ‘field list’: INSERT INTO  
`survey_possible_answers` (`answer`, `survey_question`, `position`)  
VALUES (‘Some Answer’, ‘future\_requred\_1\_q1’, 1)

---

<div class="post-metadata">

**Author:** ![mslagell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mslagell](https://www.ruby-forum.com/u/mslagell)\
**Post date:** [February 18, 2009, 8:51am UTC](https://www.ruby-forum.com/t/foxy-fixtures-making-inconsistent-ids/160520/5 "2009-02-18T08:51:40Z")

</div>

Thanks Matt, that was the problem. I’ll keep the console in mind next  
time around… it never occurred to me to try that this time.

The Survey model didn’t change. The corrected models are:

survey\_question.rb:

class SurveyQuestion \< ActiveRecord::Base  
belongs\_to :survey  
acts\_as\_list :scope =\> :survey

has\_many :survey\_possible\_answers  
end

survey\_possible\_answer.rb:

class SurveyPossibleAnswer \< ActiveRecord::Base  
belongs\_to :survey\_question  
acts\_as\_list :scope =\> :survey\_question  
end

The migrations and fixture files didn’t have to change.

Mark

---

<div class="post-metadata">

**Author:** ![mslagell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mslagell](https://www.ruby-forum.com/u/mslagell)\
**Post date:** [February 18, 2009, 8:22am UTC](https://www.ruby-forum.com/t/foxy-fixtures-making-inconsistent-ids/160520/6 "2009-02-18T08:22:16Z")

</div>

good morning,

matt’s post did fix your problems, didn’t it?

should be somethign like this then (no :order needed for  
one :survey\_question):

class SurveyPossibleAnswers \< ActiveRecord::Base  
belongs\_to :survey\_question  
acts\_as\_list :scope =\> :survey\_question  
end
