how to get request object in Model?

734 views
Skip to first unread message

Beta Beta

unread,
Dec 27, 2007, 7:25:57 PM12/27/07
to rubyonra...@googlegroups.com
Hi,

I would like to use request.host in before_save inside model. Since
request object is only available in controller and not available inside
model, I cannot use request directly in model.
Creating a parameter for before_save also not an option since every save
will need to supply paramater to before_save.

Any body has ideas how to approach this problem?
May be extending active records just like created_by and updated_by

Thanks in advance.
--
Posted via http://www.ruby-forum.com/.

Ryan Bigg

unread,
Dec 27, 2007, 7:30:57 PM12/27/07
to rubyonra...@googlegroups.com
Use a before_filter to call a method defined in the ApplicationController:

class ApplicationController < ActionController::Base

before_filter :ip_thingo

def ip_thingo
@record = Record.find(params[:id])
@record.update_attribute("ip",request.remote_addr)
end

You can put the before_filter anywhere in any of your controllers, and you can specify :only and :except to get it to run on only some actions, or all actions except some.

Beta Beta

unread,
Dec 27, 2007, 8:06:28 PM12/27/07
to rubyonra...@googlegroups.com
Ryan Bigg wrote:
> Use a before_filter to call a method defined in the
> ApplicationController:

Excellent idea Ryan. thats solved it.

Reply all
Reply to author
Forward
0 new messages