滑轨遵循Convention over Configuration
也ActiveRecord的跟随ORM(对象关系映射),并根据它:
所有models
在Rails应用程序具有Singular
类名称与在database
一个plural
名称。对于例如:在你的情况,
Department
模式将参考departments
数据库中的表,在这里你有departments
表,但不是一个model
系Rails应用程序。
By default, Active Record uses some naming conventions to find out how the mapping between models and database tables should be created. Rails will pluralize your class names to find the respective database table. So, for a class Book, you should have a database table called books. The Rails pluralization mechanisms are very powerful, being capable to pluralize (and singularize) both regular and irregular words.
现在,当您尝试初始化irb
的Department
模型像
$ > d=Department.new
这将完成是会在你app/models
目录中搜索类名作为Department
,如果找到这个类就将它与数据库中类似的departments
表相关联,并将初始化这个object
。
很明显,你会发现error
,因为你在app/models/Department.rb
中没有声明任何类别为Department
。
所以,你现在需要做的是根据@shani
的答案目录来声明一个类名作为你的模型Department
:
class Department < ActiveRecord::Base
end