# Aggregating multiple ActiveRecord relationships

**URL:** <https://www.ruby-forum.com/t/aggregating-multiple-activerecord-relationships/143022>\
**Category:** Rails\
**Created:** [July 14, 2008, 4:23pm UTC](https://www.ruby-forum.com/t/aggregating-multiple-activerecord-relationships/143022 "2008-07-14T16:23:04Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cc9497/32.png) [@jack](https://www.ruby-forum.com/u/jack)\
**Post date:** [July 14, 2008, 4:23pm UTC](https://www.ruby-forum.com/t/aggregating-multiple-activerecord-relationships/143022/1 "2008-07-14T16:23:04Z")

</div>

The following pastie will help with my question below: help:  
[http://pastie.org/219582](http://pastie.org/219582)

A user has\_many Items via both the User model as well as the user’s  
parent Account model . Other users are able to make their own Items  
visible to other users via the AccountsItemsPermission relationship.

I would like to create a “meta” relationship for the user that  
aggregates both has\_many relationships at the User and Account level  
into one, so there is an easy-to-access ‘visible\_items’ relationship  
that retrieves all accessible Items for a give user.

As you can see, I am currently using custom finder\_sql to achieve this  
goal, but would like to see if there are more elegant solutions to  
this problem that wouldn’t have to resort to finder\_sql. One of the  
disadvantages of finder\_sql is that it is incompatible with plugins  
like will\_paginate.

Any ideas would be appreciated, thanks!

Jack
