开发者

Rails naming conventions for models with forbidden names

I'm writing a rails application, and I need to name one of my model Test, and inside that model I need a class attribute - when I tried the first one, I couldn't run any UT since Test ceased to be a module - so I ranamed to Tst. I haven't even tried naming a column class - I went with clss. What names would you use? Are there any conventions k开发者_高级运维nown amongs RoR developers for such situations?


I haven't seen anything for Test but class is typically changed to klass.


Ruby and Rails have several different objects part of a standard library. You cannot use an already-defined class name as name for your models.

For instance, you cannot call a model Thread or Object because Ruby already defines a Thread and Object class. Likewise, Ruby has a library called Test::Unit so you can't use the Test namespace as model name.

There isn't a real full list of reserve objects because it really depends on your current environment. However, in order to successfully use Rails, you should at least have a basic Ruby knowledge so that you know the names of the most common standard library classes.


I've run up against this a few times (writing educational software--where you regularly want to model 'tests' :-). Depends exactly what you're modeling I suppose, but I usually opt for 'quiz' to avoid conflicts. I'd love to hear a better solution, since I find 'quizzes' an awkward plural.

Like dj2 said, class is usually done as 'klass'.


Please check the following page for Rails reserved words: http://reservedwords.herokuapp.com/ None of these words should be used as class or attribute name.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜