Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Models with reserved keywords

I want to create a model called 'File', but it is a reserved model name is rails. I can't think of anything else sane to call the model, so I was wondering if there is a standard way of dealing with this issue, for example adding a prefix or suffix (_File, FileItem, etc)?

like image 636
Cameron Martin Avatar asked Aug 05 '12 23:08

Cameron Martin


1 Answers

This problem is addressed with modules:

Modules are a way of grouping together methods, classes, and constants. Modules give you two major benefits:

  1. Modules provide a namespace and prevent name clashes.
  2. Modules implement the mixin facility.

[...]

Modules define a namespace, a sandbox in which your methods and constants can play without having to worry about being stepped on by other methods and constants.

In your case:

module MyRailsApp
  class File
  ...
  end
end

whereby your File class is used as MyRailsApp::File. This is the typical solution in Ruby, in Ruby on Rails this might be handled differently, please see the following references for an in depth discussion:

  • Handling namespace models (classes) in namespace
  • ActiveRecord: Can haz namespaces?
  • Namespaced models and controllers
  • Namespaced models
  • A simple alternative to namespaced models
like image 99
Konrad Reiche Avatar answered Oct 25 '22 02:10

Konrad Reiche