开发者

Ruby: more efficient way to convert table_name into object Model?

开发者 https://www.devze.com 2023-03-31 05:56 出处:网络
So.. I\'m trying to do something like this: \"active_record_object\" => ActiveRecordObject with the following code:

So.. I'm trying to do something like this:

"active_record_object" => ActiveRecordObject

with the following code:

ar_object = (object.singularize.split("_").each{|o| o.capitalize!}).join.constantize #开发者_高级运维convert to ActiveRecord object

is there a more efficient way to do this? chaining 7 string manipulation methods together seems a lil crazy for ruby.


There's a method called classify for that:

"active_record_object".classify
 # => "ActiveRecordObject"

Edit: as pointed out in the comments, "active_record_object".classify.constantize gets you all the way.

0

精彩评论

暂无评论...
验证码 换一张
取 消