Using a string to find a model name

Hi Rails,

I have a string in my controller that I want to make into a model name.
EG:

Class LlamaController < ApplicationController
def addsomething
model = params[:model_name]
# Here, I would like to take the string in model and use it as a
Model
@foo = Model.new
end
end

Is this possible? Is there a better way?

Thanks
–Dean

end

Is this possible? Is there a better way?

@foo = Class.const_get(model).new

I think.

Cool, thanks.

0 urzatron app/models % …/…/script/console
Loading development environment.

Class.const_get(‘User’).new

=> #<User:0xf7151cd0 @attributes={“created_on”=>nil, “updated_on”=>nil,
“password”=>"", “login”=>"", “css_style”=>“default”}, @new_record=true>

On Wednesday 25 October 2006 23:17, Dean wrote:

@foo = Model.new

end
end

Is this possible? Is there a better way?

model.constantize

or

model.camelize.constantize

Inflector magic…

Michael


Michael S.
mailto:[email protected]
http://www.schuerig.de/michael/