0
-This plugin is a
tiny addition to ActiveRecord::Base that establishes a better
0
+This plugin is a
n addition to ActiveRecord::Base that establishes a better
0
convention for finding records based on parameters. It adds a "from_param" class
0
method to ActiveRecord::Base as a convention for fetching a model from a URL
0
parameter. By default it will find a record based on the to_i of the passed in
0
-parameter, but the real use is by overriding it.
0
+parameter, but storing a parameter in a column is where it becomes especially useful.
0
+If you create a 'param' column in your table (or any other column using set_param_column),
0
+the to_param of your record will automatically be saved to that column whenever
0
+you save the record, and you will be able to retrieve it using just a simple
0
+Model.from_param call with the generated to_param. It is probably wise to add an index
0
+to the param column if you use one.
0
It's time to move away from generated-key-dependence, and this plugin is an attempt
0
to make that easy, painless, and work easily within the existing systems.
0
@@ -16,31 +22,35 @@ Example
0
class User < ActiveRecord::Base
0
+ "#{id}-#{first_name.downcase}-#{last_name.downcase}"
0
class UsersController < ApplicationController
0
+ # GET /users/1
-bobby-mcfarin0
- @user = User.from_param(params[:id]) # => <User id=1
login=mbleigh>
0
+ @user = User.from_param(params[:id]) # => <User id=1
first_name="Bobby" last_name="McFarin">
0
+ #
using a 'param' column, in this case 'slug'0
- class User < ActiveRecord::Base
0
+ class Post < ActiveRecord::Base
0
+ set_param_column "slug" # defaults to "param"
0
- def self.from_param(parameter)
0
- User.find_by_login(parameter)
0
+ "#{created_at.strftime("%Y-%m-%d")}-#{title.gsub(" ","-").downcase.gsub(/[^a-z0-9-]/,"")}"
0
- class UsersController < ApplicationController
0
- @user = User.from_param(params[:id]) # => <User id=1 login=mbleigh>
0
+ class PostsController < ApplicationController
0
+ # GET /posts/2008-04-26-from-param-plugin-released
0
+ @post = Post.from_param(params[:id]) # => <Post title="From Param: Plugin Released" created_at="2008-04-26">
0
Copyright (c) 2008 Michael Bleigh, released under the MIT license