# Selecting conditionally from HABTM

**URL:** <https://www.ruby-forum.com/t/selecting-conditionally-from-habtm/51522>\
**Category:** Rails\
**Created:** [January 2, 2006, 6:46am UTC](https://www.ruby-forum.com/t/selecting-conditionally-from-habtm/51522 "2006-01-02T06:46:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![agnathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@agnathan](https://www.ruby-forum.com/u/agnathan)\
**Post date:** [January 2, 2006, 6:46am UTC](https://www.ruby-forum.com/t/selecting-conditionally-from-habtm/51522/1 "2006-01-02T06:46:57Z")

</div>

Hello,  
I was wondering whether there is a nice Rubyesque way of selecting  
from a pair of HABTM models where one of them is inherited from  
another object.

Here is the situation:

I have a ‘posts’ table from which ‘replies’ and ‘articles’ are  
inherited. Articles have\_and\_belong\_to\_many categories, but neither  
posts nor replies do (it just does not seem to be very useful). I want  
to have a page which shows (paginatedly) the articles in a selected  
category. So given a category, I would like to return an array of all  
of the articles associated with it.

I am no master of SQL syntax, so I am not sure what to include in a  
'LEFT JOIN" statement.

Thanks for any help,

Daniel H.

---

<div class="post-metadata">

**Author:** ![agnathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@agnathan](https://www.ruby-forum.com/u/agnathan)\
**Post date:** [January 2, 2006, 11:03am UTC](https://www.ruby-forum.com/t/selecting-conditionally-from-habtm/51522/2 "2006-01-02T11:03:19Z")

</div>

Daniel,

Maybe I’m not understanding the problem correctly, but if you are  
using single-table inheritance (ie a ‘posts’ table with a ‘type’  
column that would either be ‘Post’, ‘Reply’, or ‘Article’) you can  
grab the articles associated with a selected category simply through:

@category\_object.articles.

In other words, you can refer directly to the Article class.

- Derek

On 1/2/06, Daniel H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> to have a page which shows (paginatedly) the articles in a selected  
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Derek H.  
HighGroove Studios - [http://www.highgroove.com](http://www.highgroove.com)  
Atlanta, GA  
Keeping it Simple.  
404.593.4879

---

<div class="post-metadata">

**Author:** ![agnathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@agnathan](https://www.ruby-forum.com/u/agnathan)\
**Post date:** [January 2, 2006, 7:46pm UTC](https://www.ruby-forum.com/t/selecting-conditionally-from-habtm/51522/3 "2006-01-02T19:46:58Z")

</div>

That will give me the correct articles array, but how would I pass  
that to a paginator? I have looked through the API docs and have not  
been able to find any way to tell the paginator to operate just on a  
certain array and not all Articles? It seems like there should just be  
a :collection option to take an array and paginate that.

Any help is appreciated.

Daniel H.

---

<div class="post-metadata">

**Author:** ![agnathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@agnathan](https://www.ruby-forum.com/u/agnathan)\
**Post date:** [January 2, 2006, 10:57pm UTC](https://www.ruby-forum.com/t/selecting-conditionally-from-habtm/51522/4 "2006-01-02T22:57:26Z")

</div>

Daniel,

How about:

@article\_pages, @articles = paginate :articles, :per\_page =\>  
10, :conditions =\> ‘category = category\_name’

The :conditions option gets passed on to Article.find(), effectively  
letting you specify the WHERE clause of the query it uses to get the  
Articles for display. Not exactly the ‘paginate-an-arbitrary-array’  
you’re looking for, I know, but if I understand what you’re trying to  
do, this should be an option for you.

Does that help?

David
