# Cannot get fixtures working (with has\_and\_belongs\_to\_many)

**URL:** <https://www.ruby-forum.com/t/cannot-get-fixtures-working-with-has-and-belongs-to-many/153904>\
**Category:** Rails\
**Created:** [November 22, 2008, 6:59pm UTC](https://www.ruby-forum.com/t/cannot-get-fixtures-working-with-has-and-belongs-to-many/153904 "2008-11-22T18:59:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![peter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/278dde/32.png) [@peter](https://www.ruby-forum.com/u/peter)\
**Post date:** [November 22, 2008, 6:59pm UTC](https://www.ruby-forum.com/t/cannot-get-fixtures-working-with-has-and-belongs-to-many/153904/1 "2008-11-22T18:59:27Z")

</div>

Hello,

I have the following scenario:  
class User; has\_and\_belongs\_to\_many :roles; end  
class Role; has\_and\_belongs\_to\_many :users; end  
#roles schema only has name (string) as attribute  
an additional table ‘roles\_users’ with user\_id (integer) and role\_id  
(integer) was necessary.  
Then I created a fixture in users.yml:  
admin:  
roles: admin, guest  
and in roles.yml:  
guest:  
name: guest  
admin:  
name: admin

This should work as mentioned in the docs:  
[http://api.rubyonrails.org/classes/Fixtures.html](http://api.rubyonrails.org/classes/Fixtures.html)  
But the following line did not pass:  
assert\_not\_nil users(:admin).roles.find\_by\_name(‘admin’)  
Because users(:admin).roles is empty!

For the problem I crawled a lot of docs but I cannot see where I made  
a mistake.  
Could someone lead me in the right direction?

Regards,  
Peter.

---

<div class="post-metadata">

**Author:** ![peter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/278dde/32.png) [@peter](https://www.ruby-forum.com/u/peter)\
**Post date:** [November 23, 2008, 11:23pm UTC](https://www.ruby-forum.com/t/cannot-get-fixtures-working-with-has-and-belongs-to-many/153904/2 "2008-11-23T23:23:14Z")

</div>

Hmmh,

even after cleaning up the roles\_users (without timestamp) it does not  
work (Hint from [1])  
Tried out different order of fixture importing → nothing helps.

So, I switched to the old way to define fixtures [2] and it works 🙂

Regards,  
Peter.

[1]  
[http://groups.google.com/group/rubyonrails-talk/browse\_thread/thread/ea4de9d194fefc2d/62cd3b8f365eba8b](http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/ea4de9d194fefc2d/62cd3b8f365eba8b)  
[2]  
[http://railsforum.com/viewtopic.php?id=50](http://railsforum.com/viewtopic.php?id=50)

---

<div class="post-metadata">

**Author:** ![peter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/278dde/32.png) [@peter](https://www.ruby-forum.com/u/peter)\
**Post date:** [November 23, 2008, 11:47pm UTC](https://www.ruby-forum.com/t/cannot-get-fixtures-working-with-has-and-belongs-to-many/153904/3 "2008-11-23T23:47:15Z")

</div>

On Nov 23, 2008, at 5:22 PM, Peter wrote:

> Peter.
> 
> [1]  
> [http://groups.google.com/group/rubyonrails-talk/browse\_thread/thread/ea4de9d194fefc2d/62cd3b8f365eba8b](http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/ea4de9d194fefc2d/62cd3b8f365eba8b)  
> [2]  
> [http://railsforum.com/viewtopic.php?id=50](http://railsforum.com/viewtopic.php?id=50)

This is the only message I’ve looked at, but can you turn the join  
table’s fixture into an ordered map:

— !omap

- first:  
id: “1”  
role\_id: “1”  
user\_id: “1”
- second:  
id: “2”  
role\_id: “1”  
user\_id: “2”

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![peter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/278dde/32.png) [@peter](https://www.ruby-forum.com/u/peter)\
**Post date:** [November 26, 2008, 8:57am UTC](https://www.ruby-forum.com/t/cannot-get-fixtures-working-with-has-and-belongs-to-many/153904/4 "2008-11-26T08:57:06Z")

</div>

Thanks for the answer Rob!  
The intention was to avoid the the explicit definition of a join table  
in the fixtures,  
like it is possible since some months. See 'Let‘s make the HABTM  
fixture go away. ’ in  
[http://api.rubyonrails.org/classes/Fixtures.html](http://api.rubyonrails.org/classes/Fixtures.html)

For your approach: I will put the omap into a roles\_users.yml file,  
right?

Regards,  
Peter.

On Nov 23, 11:46 pm, Rob B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![peter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/278dde/32.png) [@peter](https://www.ruby-forum.com/u/peter)\
**Post date:** [November 26, 2008, 3:46pm UTC](https://www.ruby-forum.com/t/cannot-get-fixtures-working-with-has-and-belongs-to-many/153904/5 "2008-11-26T15:46:57Z")

</div>

On Nov 26, 2008, at 2:56 AM, Peter wrote:

> Thanks for the answer Rob!  
> The intention was to avoid the the explicit definition of a join table  
> in the fixtures,  
> like it is possible since some months. See 'Let‘s make the HABTM  
> fixture go away. ’ in  
> [http://api.rubyonrails.org/classes/Fixtures.html](http://api.rubyonrails.org/classes/Fixtures.html)

Hadn’t seen that. (But I haven’t used a HABTM association in a while  
either.)

> For your approach: I will put the omap into a roles\_users.yml file,  
> right?
> 
> Regards,  
> Peter.

Yes, that’s right. I noticed that the “YAML Fixtures” section of that  
page you referenced describes the omap type also.

-Rob
