# Ferret and habtm

**URL:** <https://www.ruby-forum.com/t/ferret-and-habtm/124060>\
**Category:** Rails\
**Created:** [December 24, 2007, 6:51am UTC](https://www.ruby-forum.com/t/ferret-and-habtm/124060 "2007-12-24T06:51:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tresero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bb73d2/32.png) [@tresero](https://www.ruby-forum.com/u/tresero)\
**Post date:** [December 24, 2007, 6:51am UTC](https://www.ruby-forum.com/t/ferret-and-habtm/124060/1 "2007-12-24T06:51:52Z")

</div>

I was wondering if anyone had used ferret with an habtm relation?

Say you have books and authors  
books habtm authors  
authors habtm books

join table would be authors\_books

I know in a 1 =\> m you can:  
acts\_as\_ferret :fields =\> [:title, :author\_name] on books  
but how would you do something similar with the habtm?

Jon

---

<div class="post-metadata">

**Author:** ![tresero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bb73d2/32.png) [@tresero](https://www.ruby-forum.com/u/tresero)\
**Post date:** [December 24, 2007, 8:29pm UTC](https://www.ruby-forum.com/t/ferret-and-habtm/124060/2 "2007-12-24T20:29:14Z")

</div>

You need to return what you want ferret to return to you when a search  
is made.

The search results will return all books that contain the name of the  
author, or authors. You could have a author\_name as such,

def author\_names  
search\_string = “”  
authors = Authors.find\_all\_by\_book(self)  
authors.each do | author |  
search\_string \<\< author.name \<\< " "  
end  
search\_string  
end

You put this in the field to search and ferret will happily index it.

Serge Chevarie-Pelletier
