# Is there a way to use @today in a model?

**URL:** <https://www.ruby-forum.com/t/is-there-a-way-to-use-today-in-a-model/187136>\
**Category:** Rails\
**Created:** [April 18, 2010, 7:31am UTC](https://www.ruby-forum.com/t/is-there-a-way-to-use-today-in-a-model/187136 "2010-04-18T07:31:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [April 18, 2010, 7:31am UTC](https://www.ruby-forum.com/t/is-there-a-way-to-use-today-in-a-model/187136/1 "2010-04-18T07:31:14Z")

</div>

I’m trying to get records from a has\_many that are only from this  
year.

has\_many :visit, :conditions =\> “year = #{@today.year}”, :dependent  
=\> :destroy

This has trouble. it thinks @today is null… Any other ideas?

Bob

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [April 18, 2010, 8:50am UTC](https://www.ruby-forum.com/t/is-there-a-way-to-use-today-in-a-model/187136/2 "2010-04-18T08:50:16Z")

</div>

On Apr 17, 2010, at 10:30 PM, Bob S. wrote:

> I’m trying to get records from a has\_many that are only from this  
> year.
> 
> has\_many :visit, :conditions =\> “year = #{@today.year}”, :dependent  
> =\> :destroy
> 
> This has trouble. it thinks @today is null… Any other ideas?

Don’t you want :visits (with an s?)

has\_many :visits, :conditions =\> “year =  
SQL\_FUNCTION\_THAT\_RETURNS\_CURRENT\_YEAR”,…

Replacing “SQL\_FUNCTION…YEAR” with whatever works for your database.

If you want to specify the year, look into using a lambda for the  
conditions…

-philip

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [April 18, 2010, 4:01pm UTC](https://www.ruby-forum.com/t/is-there-a-way-to-use-today-in-a-model/187136/3 "2010-04-18T16:01:34Z")

</div>

On Sun, Apr 18, 2010 at 2:46 AM, Philip H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Don’t you want :visits (with an s?)
> 
> has\_many :visits, :conditions =\> “year =  
> SQL\_FUNCTION\_THAT\_RETURNS\_CURRENT\_YEAR”,…
> 
> Replacing “SQL\_FUNCTION…YEAR” with whatever works for your database.
> 
> If you want to specify the year, look into using a lambda for the  
> conditions…

I don’t think you can use a lambda on the :conditions option to  
find/has\_many…

but you can do it with a named scope

has\_many :visits, :dependent =\> destroy  
named\_scope :this\_year, lambda { { :conditions =\> { :year =\>  
Date.today.year } }}

Then to get the visits this year for a model referred to by the variable  
model

model.visits.this\_year

Alternatively, you could have a slightly different named scope which  
would let you specify the year at run-time, assuming that @today might  
not always Date.today, e.g. if you are using the user’s timezone.

named\_scope :in\_year, lambda { |year| {:conditions =\> {:year =\> year}}

Then you could use

model.visits.in\_year(@today.year)

The reason you need to express this a a lambda is twofold:

1. has\_many and named\_scope are evaluated in the context of the active  
record class, so @today is not an instance variable but a class  
instance variable, which is probably why it is nil.

2. more importantly, @today is being evaluated at the time the  
association declaration is executed (during class definition time). If  
you want to use the value at the time you make the query, it needs to  
be a lambda so that the value gets evaluated each time.

## HTH

Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Github: [rubyredrick (Rick DeNatale) · GitHub](http://github.com/rubyredrick)  
Twitter: @RickDeNatale  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [April 18, 2010, 10:20am UTC](https://www.ruby-forum.com/t/is-there-a-way-to-use-today-in-a-model/187136/4 "2010-04-18T10:20:59Z")

</div>

On 18 April 2010 06:30, Bob S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m trying to get records from a has\_many that are only from this  
> year.
> 
> has\_many :visit, :conditions =\> “year = #{@today.year}”, :dependent  
> =\> :destroy
> 
> This has trouble. it thinks @today is null… Any other ideas?

What is @today?

Colin

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [April 18, 2010, 5:49pm UTC](https://www.ruby-forum.com/t/is-there-a-way-to-use-today-in-a-model/187136/5 "2010-04-18T17:49:52Z")

</div>

try using a range in the condition…  
Post.all(:conditions=\>{:created\_at=\>Time.now.beginning\_of\_year…Time.now.end\_of\_year})

you can also create a named scope (rails 2.3) in the belongs\_to end of  
the relationship  
class Post \< ActiveRecord::Base  
named\_scope :from\_this\_year, lambda { { :conditions=\>{:created\_at =\>  
Time.now.beginning\_of\_year…Time.now.end\_of\_year}}  
end  
…you need to use a lambda, otherwise the time value from app load is  
used… which obviously wouldn’t be what you want.

you would use it like so  
User.first.posts.from\_this\_year

or you could set up a has\_many  
class User \< ActiveRecord::Base

has\_many :posts\_from\_this\_year, :class\_name=\>“Post”,  
:conditions=\>{:created\_at=\>Time.now.beginning\_of\_year…Time.now.end\_of\_year}  
end  
@user.posts\_from\_this\_year
