Snippets: Rails to_param

August 09, 2007 @ 05:08 PM | posted by carmelyne

(last updated: 08.09.07)

ID + permalink. The id gets a to_i

1
2
3
4
5
6
7
8
9
# using .join
def to_param
  [id, permalink].join('-')
end

# just another way to do it
def to_param
  "#{id}-#{permalink}"
end

[ Last updated: August 09, 2007 @ 06:27 PM ]


1 Response to...
“Snippets: Rails to_param”

  1. nicolash:

    using * to join

    def to_param [id, permalink] * '-' end

    ;-)



    Posted:

Sorry, comments are closed for this article.

Snippets of 06/24/07

Rails 'A'..'Z' Paginate

1
2
3
4
5
6
7
8
9
10
11
# Starts with 'A'..'Z' Paginate / model
def self.sort(sort)
  if sort
    find(:all, :conditions => ['name LIKE ?', "#{sort}%"])
  else
    find(:all, :order => 'name')
  end  
end

# index action / controller
@models = Model.sort(params[:sort]) 

Recent Posts...

RailsConf 2006
I heart devChix