tribalvibes
tribalvibes

Reputation: 2107

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.

Upvotes: 6

Views: 2446

Answers (2)

mnishiguchi
mnishiguchi

Reputation: 2241

This worked for me with Ruby 2.5.0 and Rails 5.1.6.

"Admin::Role".parameterize.underscore
#=> "admin_role"

Upvotes: 1

deb
deb

Reputation: 12824

Have you tried parameterize with an underscore as a separator?

parameterize('_')

Upvotes: 9

Related Questions