# Conditions on Has\_many :Through

**URL:** <https://www.ruby-forum.com/t/conditions-on-has-many-through/145308>\
**Category:** Rails\
**Created:** [August 7, 2008, 8:05pm UTC](https://www.ruby-forum.com/t/conditions-on-has-many-through/145308 "2008-08-07T20:05:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![morgan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/0ea827/32.png) [@morgan](https://www.ruby-forum.com/u/morgan)\
**Post date:** [August 7, 2008, 8:05pm UTC](https://www.ruby-forum.com/t/conditions-on-has-many-through/145308/1 "2008-08-07T20:05:03Z")

</div>

so i have a bunch of users  
this bit of code gets all the schedules for a particular user. i’m  
wondering if there’s a way for me to get only a range of schedules based  
on the time field for the user without having to get all the schedules  
assigned to the user the user then stepping through the time for every  
schedule.

this is the bit now.

```
    @blab = User.find(session[:user].id).myschedules

   -step through each schedule that the user has and check to make

```

sure that the schedule is in the specified date and time range before  
displaying each schedule. (yes i know this is very sloppy.

i would like to be able to  
@blab = User.find(session[:user].id).myschedules.find(from  
1200am to 9pm)

```
   -step throught each schedule that the user has.

```

or something to that effect

btw myschedules the has\_many through relationship.

---

<div class="post-metadata">

**Author:** ![morgan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/0ea827/32.png) [@morgan](https://www.ruby-forum.com/u/morgan)\
**Post date:** [August 8, 2008, 5:15pm UTC](https://www.ruby-forum.com/t/conditions-on-has-many-through/145308/2 "2008-08-08T17:15:07Z")

</div>

nobody?

---

<div class="post-metadata">

**Author:** ![morgan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/0ea827/32.png) [@morgan](https://www.ruby-forum.com/u/morgan)\
**Post date:** [August 8, 2008, 5:30pm UTC](https://www.ruby-forum.com/t/conditions-on-has-many-through/145308/3 "2008-08-08T17:30:17Z")

</div>

You should do something like this:

first of all I’d suggest you remove MY from model’s name (so instead  
of MySchedules to be only Schedule).

In Schedule model (schedule.rb) you have to make a finder:

def self.between(from, to)  
self.find :all, :conditions =\> [“happens\_on BETWEEN ? AND ?”, from,  
to]  
end

Then you can do find like this:

person = Person.find 1  
person.schedules.between(@from, @to)

Hope this helps.  
On 8 avg., 17:15, Morgan M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![morgan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/0ea827/32.png) [@morgan](https://www.ruby-forum.com/u/morgan)\
**Post date:** [August 8, 2008, 9:18pm UTC](https://www.ruby-forum.com/t/conditions-on-has-many-through/145308/4 "2008-08-08T21:18:43Z")

</div>

you can use a named scope for that
