# Trying the ol' articles route, need some help

**URL:** <https://www.ruby-forum.com/t/trying-the-ol-articles-route-need-some-help/160348>\
**Category:** Rails\
**Created:** [February 15, 2009, 2:32am UTC](https://www.ruby-forum.com/t/trying-the-ol-articles-route-need-some-help/160348 "2009-02-15T02:32:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![yaphi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/e274bd/32.png) [@yaphi](https://www.ruby-forum.com/u/yaphi)\
**Post date:** [February 15, 2009, 2:32am UTC](https://www.ruby-forum.com/t/trying-the-ol-articles-route-need-some-help/160348/1 "2009-02-15T02:32:55Z")

</div>

Hey there,

I am trying to figure out routing and thought that this would probably  
be the best way to learn how to do some complicated routes. I’ve  
looked at a lot of people’s methods for getting the /articles/  
2009/02/12/some-title to work but they all seem to forget to post some  
custom method they’re writing like by\_date.

So here was my stab at it, I know I need to gsub the title. I just  
don’t know where (in the conditions?)

`
// routes.rb
map.article_page 'articles/:year/:month/:title', :controller =>
'articles', :action => "show",
    :requirements => { :year => /(19|20)\d\d/, :month => /[01]?\d/ }
// articles_controller.rb
def show
@article = Article.find_by_date(params)
…
// article.rb
def self.find_by_date(params)
find(:first, :conditions => [“publish_date.year = ? and
publish_date.month = ? and title = ?”, params[:year], params[:month],
params[:title]])
end
`

I know publish\_date.year is wrong but I can’t figure out a way to pull  
the year out of the publish\_date column here.

Any help would be surely appreciated.

---

<div class="post-metadata">

**Author:** ![yaphi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/e274bd/32.png) [@yaphi](https://www.ruby-forum.com/u/yaphi)\
**Post date:** [February 15, 2009, 8:33pm UTC](https://www.ruby-forum.com/t/trying-the-ol-articles-route-need-some-help/160348/2 "2009-02-15T20:33:15Z")

</div>

Thank you very much Maurício!

I’ll give that a shot.

On Feb 15, 11:17 am, Maurício Linhares [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![yaphi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/e274bd/32.png) [@yaphi](https://www.ruby-forum.com/u/yaphi)\
**Post date:** [February 15, 2009, 5:18pm UTC](https://www.ruby-forum.com/t/trying-the-ol-articles-route-need-some-help/160348/3 "2009-02-15T17:18:04Z")

</div>

Hi Yaphi,

First, you shoudn’t “gsub” the title, you must transform it into a  
“permalink”. A simple way to do this is to use the permalink\_fu plugin  
 → [GitHub - technoweenie/permalink\_fu: ActiveRecord plugin for automatically converting fields to permalinks.](http://github.com/technoweenie/permalink_fu/tree/master)

Having it installed, here’s how your model could look like:

class Article \< ActiveRecord::Base

#you’ll need a “title\_permalink” column at your “posts” table to  
hold the permalink value  
#this method creates a permalink base on the :year, :month and  
:title properties  
has\_permalink [:year, :month, :title], :title\_permalink

def year  
publish\_date.year if publish\_date  
end

def month  
publish\_date.month if publish\_date  
end

class \<\< self  
# this method finds the record with the specified permalink, you  
can even pass the params hash directly  
def find\_by\_permalink( options )  
first( :conditions =\> { :title\_permalink =\>  
“#{options[:year]}-#{options[:month]}-#{options[:title]}” } ) ||  
raise( ActiveRecord::RecordNotFound, options.inspect )  
end

end

end

#at your controller  
def show  
@post = Article.find\_by\_permalink( options )  
end

- 

Maurício Linhares  
[http://alinhavado.wordpress.com/](http://alinhavado.wordpress.com/) (pt-br) | [http://blog.codevader.com/](http://blog.codevader.com/)  
(en)
