Forum: Ruby on Rails Question for method_missing

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Marcelo J. (Guest)
on 2007-02-03 19:33
Well, I want to make a method that return a model through a session. For
example. When I call "obj = session_object_Usuario", return a model
Usuario, through session "usuario_id". I think about something as:

 def method_missing("session_object_"model)
  model.find(session[":#{model}_id"])
 end

And place this in application.rb.

How make this?
Zack C. (Guest)
on 2007-02-03 20:00
(Received via mailing list)
On 2/3/07, Marcelo J. <removed_email_address@domain.invalid> wrote:
>
> How make this?

Marcelo,

I'm trying to figure out why you'd want to use method_missing to load
an object from the session...

If you're already typing:

obj = session_object_Usuario

Then it seems easier, faster, and more secure to define an accessor:

# application.rb

def current_usuario
  @current_usuario ||= (session[:usuario_id] ?
Usuario.find(session[:usuario_id]) : nil)
end
helper_method :current_usuario

Hope this helps.

--
Zack C.
http://depixelate.com
Marcelo J. (Guest)
on 2007-02-03 20:10
Yes, this helps....thanks!

But I would like to make using method_missing, therefore he would be
more generic.
This topic is locked and can not be replied to.