Is there a better way to get the underscore-case version of an ActiveRecord model's name? So far this works, but it's far from ideal:
You can use the
singular method of ActiveModel::Naming:
Alternatively, you can use the
model_name method that's mixed in to ActiveRecord by the same
ActiveModel::Naming module. However, it's not available on a record instance, so you still have to go through its class:
or call it directly on the class:
ActiveModel::Name instance returned by
model_name contains many other inflections and name-related stuff.
You might consider making a module or concern that defines a
name_for_path (or something) method, and which you can then include in the relevant models, so you can just call that.
By the way, your solution is not uncommon. I looked at the source for the Paperclip gem, which constructs file paths from model names, and it does pretty much the same thing to get the model's name, (though I don't know if there's a specific reason for it, or if they too could just as well use the approaches above).
However, if you keep your version, you should probably use
to_s like Paperclip does: