# Ruby on rails syntax

**URL:** <https://www.ruby-forum.com/t/ruby-on-rails-syntax/86686>\
**Category:** Rails\
**Created:** [February 24, 2007, 4:15pm UTC](https://www.ruby-forum.com/t/ruby-on-rails-syntax/86686 "2007-02-24T16:15:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jon](https://www.ruby-forum.com/u/jon)\
**Post date:** [February 24, 2007, 4:15pm UTC](https://www.ruby-forum.com/t/ruby-on-rails-syntax/86686/1 "2007-02-24T16:15:13Z")

</div>

Hi everyone, simple syntax question that is confusing me.  
I have the folowing method im my controller:

def archive  
@posts = Post.find(:all, :conditions =\> [“YEAR(created\_at) = ? and  
MONTH(created\_at) = ?”, params[:year], params[:month]])  
end

which works fine.

but if use similar code in my view, it does not work, e.g

\<%= YEAR(post.created\_at) %\>

i have to use this code in my view:

\<%= post.created\_at.year %\>

But likewise, if use this line in my controller, it does not work:

def archive  
@posts = Post.find(:all, :conditions =\> [“created\_at.year = ? and  
created\_at.month = ?”, params[:year], params[:month]])  
end

can anyone explain this to me please?

many thanks

---

<div class="post-metadata">

**Author:** ![jon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jon](https://www.ruby-forum.com/u/jon)\
**Post date:** [February 24, 2007, 4:28pm UTC](https://www.ruby-forum.com/t/ruby-on-rails-syntax/86686/2 "2007-02-24T16:28:42Z")

</div>

Hi –

On Sat, 24 Feb 2007, Jon wrote:

> but if use similar code in my view, it does not work, e.g  
> @posts = Post.find(:all, :conditions =\> [“created\_at.year = ? and  
> created\_at.month = ?”, params[:year], params[:month]])  
> end
> 
> can anyone explain this to me please?

The :conditions clause in ActiveRecord uses SQL, not Ruby. The string  
interpolation (\<%= … %\>) in ERb uses Ruby, not SQL. So you need to  
use both, depending on the requirements of the given library.

David

–  
Q. What is THE Ruby book for Rails developers?  
A. RUBY FOR RAILS by David A. Black ([http://www.manning.com/black](http://www.manning.com/black))  
(See what readers are saying! [http://www.rubypal.com/r4rrevs.pdf](http://www.rubypal.com/r4rrevs.pdf))  
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?  
A. Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![jon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jon](https://www.ruby-forum.com/u/jon)\
**Post date:** [February 24, 2007, 8:12pm UTC](https://www.ruby-forum.com/t/ruby-on-rails-syntax/86686/3 "2007-02-24T20:12:12Z")

</div>

Jon wrote:

> Thank you David, so the month functions being called are in fact  
> different functions?

Yes.

When you do this:

Post.find(:all, :conditions =\> [“YEAR(created\_at) = ? and  
MONTH(created\_at) = ?”, params[:year], params[:month]])

Rails sends a query like this to the database:

SELECT \* FROM posts WHERE  
YEAR(created\_at) = ‘2007’ and  
MONTH(created\_at) = ‘02’

Your database responds with whatever is appropriate to that query. The  
YEAR and MONTH function in that query are interpreted and execute by the  
database, rails just passes it along. Rails has no idea what that query  
really means, it just collects the result.

When you do:

\<%= YEAR(post.created\_at) %\>

Rails will look for a ruby method called “YEAR” and send it an argument  
of a Time object. Ruby does not find a method called “YEAR” and throws  
an exception.

You probably want something more like:

\<%= post.created\_at.year %\>

---

<div class="post-metadata">

**Author:** ![jon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jon](https://www.ruby-forum.com/u/jon)\
**Post date:** [February 24, 2007, 9:36pm UTC](https://www.ruby-forum.com/t/ruby-on-rails-syntax/86686/4 "2007-02-24T21:36:53Z")

</div>

got it! thanks guys.

---

<div class="post-metadata">

**Author:** ![jon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jon](https://www.ruby-forum.com/u/jon)\
**Post date:** [February 24, 2007, 6:20pm UTC](https://www.ruby-forum.com/t/ruby-on-rails-syntax/86686/5 "2007-02-24T18:20:30Z")

</div>

Thank you David, so the month functions being called are in fact  
different functions?
