# Has\_many through - how to use middle model's info?

**URL:** <https://www.ruby-forum.com/t/has-many-through-how-to-use-middle-models-info/145776>\
**Category:** Rails\
**Created:** [August 13, 2008, 9:04pm UTC](https://www.ruby-forum.com/t/has-many-through-how-to-use-middle-models-info/145776 "2008-08-13T21:04:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [August 13, 2008, 9:04pm UTC](https://www.ruby-forum.com/t/has-many-through-how-to-use-middle-models-info/145776/1 "2008-08-13T21:04:49Z")

</div>

Hey,

How can I use the information in the “middle” table of a :has\_many  
through relationship? Here’s an example:

Club and Student have a :has\_many through relationship with each other.

class Club \< ActiveRecord::Base  
has\_many :memberships, :dependent =\> :destroy  
has\_many :students, :through =\> :memberships  
end

class Student \< ActiveRecord::Base  
has\_many :memberships, :dependent =\> :destroy  
has\_many :clubs, :through =\> :memberships  
end

# middle model

class Membership \< ActiveRecord::Base  
belongs\_to :student  
belongs\_to :club  
end

Here is the table for “memberships”  
id - integer  
club\_id - integer  
student\_id - integer  
joined\_on - datetime

If I have @club\_id, and I want to find all students that joined that  
club on a certain date, how can I do that?

How can I do something like this  
Club.find\_by\_id(@club\_id, :include =\> :students)  
and specify the “joined\_on” date?

Thanks for any help.

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [August 13, 2008, 9:12pm UTC](https://www.ruby-forum.com/t/has-many-through-how-to-use-middle-models-info/145776/2 "2008-08-13T21:12:01Z")

</div>

To get the memberships, you can:

@memberships = @club.memberships.all(:conditions =\> {:created\_on =\>  
date})

Then now you simply have to get the students of each membership.

On Thu, Aug 14, 2008 at 3:04 AM, Joe P.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> has\_many :students, :through =\> :memberships  
> belongs\_to :club
> 
> How can I do something like this  
> Club.find\_by\_id(@club\_id, :include =\> :students)  
> and specify the “joined\_on” date?
> 
> ## Thanks for any help.
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Ramon T.
