# Books belongs\_to authors. How to find only author with books

**URL:** <https://www.ruby-forum.com/t/books-belongs-to-authors-how-to-find-only-author-with-books/139212>\
**Category:** Rails\
**Created:** [June 1, 2008, 10:15pm UTC](https://www.ruby-forum.com/t/books-belongs-to-authors-how-to-find-only-author-with-books/139212 "2008-06-01T22:15:14Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ferblape](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/73ab20/32.png) [@ferblape](https://www.ruby-forum.com/u/ferblape)\
**Post date:** [June 1, 2008, 10:15pm UTC](https://www.ruby-forum.com/t/books-belongs-to-authors-how-to-find-only-author-with-books/139212/1 "2008-06-01T22:15:14Z")

</div>

Hello

I have a table called Books  
And a table called Authors

Books belong\_to authors

How can I find only authors that have books?

Thanks

---

<div class="post-metadata">

**Author:** ![ferblape](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/73ab20/32.png) [@ferblape](https://www.ruby-forum.com/u/ferblape)\
**Post date:** [June 1, 2008, 11:08pm UTC](https://www.ruby-forum.com/t/books-belongs-to-authors-how-to-find-only-author-with-books/139212/2 "2008-06-01T23:08:29Z")

</div>

Fernando B. wrote:

> Hello
> 
> I have a table called Books  
> And a table called Authors
> 
> Books belong\_to authors
> 
> How can I find only authors that have books?
> 
> Thanks

I think an author can write many books  
so that you can put has\_many :books in Author ActiveRecord

so that you can find books easily

@author = Author.find(:first)

@books = @author.books

Reinhart  
[http://teapoci.blogspot.com](http://teapoci.blogspot.com)

---

<div class="post-metadata">

**Author:** ![ferblape](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/73ab20/32.png) [@ferblape](https://www.ruby-forum.com/u/ferblape)\
**Post date:** [June 1, 2008, 11:30pm UTC](https://www.ruby-forum.com/t/books-belongs-to-authors-how-to-find-only-author-with-books/139212/3 "2008-06-01T23:30:45Z")

</div>

I thought I’d point out too that one book can have multiple authors, so  
you  
might need something like

Book  
id  
name

Writings  
book\_id  
author\_id

Author  
id  
name

and use something like

Books has\_many :authors, :through =\> :writings  
Writings belongs\_to :book, belongs\_to :author  
Author has\_many :books, :through =\> :writings

To get that many to many relationship working.

As for your question, and using the above, now all you need to do is  
construct a find query to get authors if COUNT(\*) FROM writings WHERE  
author\_id = [id here] and check its size is \> 0

That would be my solution anyway. A more exp. rails dev might have a  
faster  
option.

Regards  
Kieran

On Mon, Jun 2, 2008 at 9:08 AM, Rails T. \<

---

<div class="post-metadata">

**Author:** ![ferblape](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/73ab20/32.png) [@ferblape](https://www.ruby-forum.com/u/ferblape)\
**Post date:** [June 2, 2008, 4:35am UTC](https://www.ruby-forum.com/t/books-belongs-to-authors-how-to-find-only-author-with-books/139212/4 "2008-06-02T04:35:41Z")

</div>

This is not optimal code, but you can make a method in Author:

def self.find\_all\_with\_books  
authors = []  
for author in self.find(:all)  
authors \<\< author if author.books.count \> 0  
end  
authors  
end

If you’re using Rails 2.1 you can check out named\_scope. Not familiar  
with it yet, but it goes something like this

class Author \< …  
has\_many :books  
named\_scope :published, :include =\> :books, :conditions =\>  
[“books.count \> 0”]

I’m pretty sure the code won’t work. Watch this to be more familiar  
with it though [#108 named\_scope - RailsCasts](http://railscasts.com/episodes/108)

On Mon, Jun 2, 2008 at 4:15 AM, Fernando B.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## Thanks
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Ramon T.

---

<div class="post-metadata">

**Author:** ![ferblape](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/73ab20/32.png) [@ferblape](https://www.ruby-forum.com/u/ferblape)\
**Post date:** [June 2, 2008, 2:51pm UTC](https://www.ruby-forum.com/t/books-belongs-to-authors-how-to-find-only-author-with-books/139212/5 "2008-06-02T14:51:02Z")

</div>

On Mon, Jun 2, 2008 at 8:27 AM, sullivan.t [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I like that, though what about finding the db records with sql and a  
> standard table join?
> 
> I suppose it’d be the same in terms of cpu cycles, etc.

No it wouldn’t, doing it in SQL would result in one query and less  
cycles.

Something like:

Author.find(:all, :joins =\> :books)

Should do the trick, this should do a query with an inner equijoin on  
Authors and Books, so Authors without books would not be returned.

Note that I haven’t actually tested this code.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![ferblape](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/73ab20/32.png) [@ferblape](https://www.ruby-forum.com/u/ferblape)\
**Post date:** [June 2, 2008, 2:28pm UTC](https://www.ruby-forum.com/t/books-belongs-to-authors-how-to-find-only-author-with-books/139212/6 "2008-06-02T14:28:23Z")

</div>

I like that, though what about finding the db records with sql and a  
standard table join?

I suppose it’d be the same in terms of cpu cycles, etc.

On Jun 1, 10:34 pm, “Ramon Miguel M. Tayag” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![ferblape](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/73ab20/32.png) [@ferblape](https://www.ruby-forum.com/u/ferblape)\
**Post date:** [June 2, 2008, 2:59pm UTC](https://www.ruby-forum.com/t/books-belongs-to-authors-how-to-find-only-author-with-books/139212/7 "2008-06-02T14:59:52Z")

</div>

On 2 Jun 2008, at 13:50, Rick DeNatale wrote:

> Something like:
> 
> Author.find(:all, :joins =\> :books)
> 
> Should do the trick, this should do a query with an inner equijoin  
> on Authors and Books, so Authors without books would not be returned.
> 
> Note that I haven’t actually tested this code.

There’s always the cheat’s version: have a counter\_cache on books.

Fred

---

<div class="post-metadata">

**Author:** ![ferblape](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/73ab20/32.png) [@ferblape](https://www.ruby-forum.com/u/ferblape)\
**Post date:** [June 2, 2008, 3:34pm UTC](https://www.ruby-forum.com/t/books-belongs-to-authors-how-to-find-only-author-with-books/139212/8 "2008-06-02T15:34:16Z")

</div>

Hey everyone! Well what do you know? I had to do this today, right  
after I emailed.

If you’re going to use named\_scope, you have to setup a counter cache.

> **[\#23 Counter Cache Column - RailsCasts](http://railscasts.com/episodes/23-counter-cache-column)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

then, in Author:

named\_scope :published, :conditions =\> [“books\_count \> 0”]

if not named\_scope, try:

Author.find(:all, :conditions =\> [“books\_count \> 0”])

Either way, counter\_cache would be a good thing to setup since doing  
these kinds of finds will be a lot less CPU intensive.

–  
Ramon T.
