# ActiveRecord query

**URL:** <https://www.ruby-forum.com/t/activerecord-query/120590>\
**Category:** Rails\
**Created:** [November 18, 2007, 10:59pm UTC](https://www.ruby-forum.com/t/activerecord-query/120590 "2007-11-18T22:59:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![nfessel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/a4c791/32.png) [@nfessel](https://www.ruby-forum.com/u/nfessel)\
**Post date:** [November 18, 2007, 10:59pm UTC](https://www.ruby-forum.com/t/activerecord-query/120590/1 "2007-11-18T22:59:56Z")

</div>

Hello,

I am trying to construct an ActiveRecord query to do the following:

I have Tags that have a many-to-many relationship with Ledgers. Many  
Ledgers can have many Tags. I want to construct a query to get all the  
associated Ledgers that have a particular Tag.

For example, I would like to get all Ledgers with the Tag “Food”. My  
database tables are as follows:

Table Ledgers  
Table Tags  
Table Ledgers\_Tags

I also have a Tag Model and a Ledger Model.

How do I construct the query using ActiveRecord?

---

<div class="post-metadata">

**Author:** ![nfessel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/a4c791/32.png) [@nfessel](https://www.ruby-forum.com/u/nfessel)\
**Post date:** [November 18, 2007, 11:19pm UTC](https://www.ruby-forum.com/t/activerecord-query/120590/2 "2007-11-18T23:19:17Z")

</div>

Never mind. I figured it out:

@ledgers = Label.find\_by\_id(params[:id]).ledgers

Thanks anyway.
