# Belongs\_to and has\_many

**URL:** <https://www.ruby-forum.com/t/belongs-to-and-has-many/183349>\
**Category:** Rails\
**Created:** [February 12, 2010, 7:12am UTC](https://www.ruby-forum.com/t/belongs-to-and-has-many/183349 "2010-02-12T07:12:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![charanya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b5e925/32.png) [@charanya](https://www.ruby-forum.com/u/charanya)\
**Post date:** [February 12, 2010, 7:12am UTC](https://www.ruby-forum.com/t/belongs-to-and-has-many/183349/1 "2010-02-12T07:12:34Z")

</div>

Hi all

I have a model called,article which belongs\_to user  
so in article.rb  
belongs\_to :user

and in user.rb  
has\_many :posts, :dependent =\> :nullify, :class\_name =\> ‘Article’

so now @user.posts is giving the posts of the user

Now like,without using any named\_scope,  
i would want @user.posts to give out only the posts which were published  
before 10 days.

I know i can use a named\_Scope  
But i want to directly use @user.posts alone to get the above result.

Is it possible to do this??

Thanks In Advance,

Charanya

---

<div class="post-metadata">

**Author:** ![charanya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b5e925/32.png) [@charanya](https://www.ruby-forum.com/u/charanya)\
**Post date:** [February 12, 2010, 9:37am UTC](https://www.ruby-forum.com/t/belongs-to-and-has-many/183349/2 "2010-02-12T09:37:15Z")

</div>

> Is it possible to do this??  
> Look up “default\_scope”

---

<div class="post-metadata">

**Author:** ![charanya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b5e925/32.png) [@charanya](https://www.ruby-forum.com/u/charanya)\
**Post date:** [February 12, 2010, 2:49pm UTC](https://www.ruby-forum.com/t/belongs-to-and-has-many/183349/3 "2010-02-12T14:49:11Z")

</div>

has\_many :posts, :conditions =\> ‘created\_at \>= #{10.days.ago}’

---

<div class="post-metadata">

**Author:** ![charanya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b5e925/32.png) [@charanya](https://www.ruby-forum.com/u/charanya)\
**Post date:** [February 12, 2010, 3:36pm UTC](https://www.ruby-forum.com/t/belongs-to-and-has-many/183349/4 "2010-02-12T15:36:35Z")

</div>

That’ll return all posts that were created in the 10 days before the  
application was started (because 10.days.ago is evaluated in the class  
body). To return all posts that were created in the 10 days before the  
query is run, you want:

has\_many :posts, :conditions =\> proc { [‘created\_at \>= ?’, 10.days.ago]  
}

Mat

On Fri, Feb 12, 2010 at 05:51, Jared Armstrong
