简体   繁体   English

如何在Rails中创建“文件”模型?

[英]How to create a 'file' model in Rails?

So I have built an API using Ruby/Sinatra and MondoDB. 因此,我使用Ruby / Sinatra和MondoDB构建了一个API。 I'm now writing an Admin panel using Rails that connects to the same database. 我现在正在使用连接到同一数据库的Rails编写一个管理面板。 One of our collections is named 'files' since we need to model an object of such kind in our API. 我们的一个集合称为“文件”,因为我们需要在API中为此类对象建模。 Now, when I wanted to create the File model in the Admin Panel, I came across the fact that I can't create a class called 'File'. 现在,当我想在管理面板中创建文件模型时,遇到了一个事实,即我无法创建一个名为“文件”的类。 Doing so results in the error attached 这样做会导致出现错误 在此处输入图片说明

Even 'File1' is an acceptable class name. 甚至“ File1”也是可接受的类名。 How do I now create the File model in my Rails app? 现在如何在Rails应用程序中创建文件模型? I'm using Rails 5.0.0.1. 我正在使用Rails 5.0.0.1。

I believe that is happening because File is a reserve Rails word. 我相信这是因为File是Rails的保留字。 I would probably rename the model to something like FileResource . 我可能会将模型重命名为FileResource

List of Reserved words: http://www.rubymagic.org/posts/ruby-and-rails-reserved-words 保留字列表: http : //www.rubymagic.org/posts/ruby-and-rails-reserved-words

The file is ruby's class, you can't and should't use the name in model. file是ruby的类,您不能也不应在模型中使用该名称。 Or you will replace the original File . 否则,您将替换原始File

FileResource is a good name! FileResource是个好名字!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM