# FactoryGirl and habtm associations

**URL:** <https://www.ruby-forum.com/t/factorygirl-and-habtm-associations/179448>\
**Category:** Rails\
**Created:** [November 25, 2009, 9:43am UTC](https://www.ruby-forum.com/t/factorygirl-and-habtm-associations/179448 "2009-11-25T09:43:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![eltercero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e99b99/32.png) [@eltercero](https://www.ruby-forum.com/u/eltercero)\
**Post date:** [November 25, 2009, 9:43am UTC](https://www.ruby-forum.com/t/factorygirl-and-habtm-associations/179448/1 "2009-11-25T09:43:57Z")

</div>

hi,

Does anyone know whether FactoryGirl is possible to generate a factory  
for a model that now HBTM associations and moreover this model requires  
the presence of this association.

If possible, would appreciate any information.

## The models:

Class Document \< Activ…  
has\_and\_belongs\_to\_many :languages  
validate\_presences\_of :languages  
end

## Class Language \< Active … has\_and\_belongs\_to\_many :documents end

Thanks.

PS: Sorry for my English.

---

<div class="post-metadata">

**Author:** ![eltercero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e99b99/32.png) [@eltercero](https://www.ruby-forum.com/u/eltercero)\
**Post date:** [November 25, 2009, 11:57am UTC](https://www.ruby-forum.com/t/factorygirl-and-habtm-associations/179448/2 "2009-11-25T11:57:38Z")

</div>

Hi

```
     You can use like in document_test

```

@document = Factory(:document,:languages =\> [Factory(:language,:name =\>  
‘English’),Factory(:language,:name =\> ‘French’)])

```
 And in this example you have factories/document.rb

```

Factory.define :language do |l|  
l.name “Name here”  
end

```
         There may be some other easy methods also which I am 

```

unaware of

Sijo

---

<div class="post-metadata">

**Author:** ![eltercero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e99b99/32.png) [@eltercero](https://www.ruby-forum.com/u/eltercero)\
**Post date:** [November 26, 2009, 12:48pm UTC](https://www.ruby-forum.com/t/factorygirl-and-habtm-associations/179448/3 "2009-11-26T12:48:39Z")

</div>

Ok,

A lot of thanks.

Or

“Motes gracies des de BCN” in Catalan.

Bye.
