Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the proper Rails inflection of underscore 'underscoreize'?

It seems that with Rails/AR and the Inflector methods added to String by ActiveSupport, I would expect that by default,

Nested::ClassDerived::FromAR.name.tableize == Nested::ClassDerived::FromAR.table_name

But actually tableize calls underscore, and underscore doesn't actually underscore the :: nested class separator, instead replacing it with a / to make a pathname. Perhaps this method should be called pathify?

Anyway, I need actual underscores. So I'm thinking of defining a new String inflector method:

def new_inflector
  underscore.gsub('/', '_')
end

that would actually underscore the nested class name string.

So, my question is, what is the proper inflection of 'underscore' in order to properly and conventionally name my new inflector method, without configuration. Would it be 'underscoreize' (following the convention established by 'tableize') or 'underscorize'? Or perhaps underscoreify?

Any insight appreciated.

like image 203
tribalvibes Avatar asked Jul 27 '11 01:07

tribalvibes


2 Answers

Have you tried parameterize with an underscore as a separator?

parameterize('_')
like image 64
deb Avatar answered Oct 18 '22 08:10

deb


This worked for me with Ruby 2.5.0 and Rails 5.1.6.

"Admin::Role".parameterize.underscore
#=> "admin_role"
like image 34
mnishiguchi Avatar answered Oct 18 '22 08:10

mnishiguchi