简体   繁体   English

Ruby on Rails使用预先存在的数据库。 Rails使一切变得复数

[英]Ruby on Rails working with pre-existing database. Rails makes everything plural

What is the deal with this? 这有什么关系? I'm working with a pre-existing that I did not do myself. 我正在处理一个我没有做过的预先存在的事情。 Everything in the database is labeled in singular form. 数据库中的所有内容都以单数形式标记。 user, security, spec, etc. I guess the right way would be users, securities, specs. 用户,安全,规格等我认为正确的方式将是用户,证券,规格。 At least that's what ruby on rails try's to lookup when I generate a scaffold . 至少那就是当我生成一个脚手架时,ruby on rails尝试查找。

How do I specifically state to use user instead of users in the sql. 我如何明确声明在sql中使用用户而不是用户。 I don't see anywhere in my project where it is looking up the sql. 我在项目的任何地方都没有看到它在查找sql​​。 I mean if my model is user you would think it would try to lookup user. 我的意思是如果我的模型是用户,你会认为它会尝试查找用户。 Instead of users. 而不是用户。

Thanks for any help. 谢谢你的帮助。

You need set_table_name :name_of_the_table in your model ( source ). 您需要在模型中使用set_table_name :name_of_the_tablesource )。

So: 所以:

class User < ActiveRecord::Base
  set_table_name :user
end

The reason they use plural for the table and singular for the model is because an instance of the model represents one user, whereas the table contains all the users. 他们为表使用复数而对模型使用单数的原因是因为模型的实例代表一个用户,而表包含所有用户。 It's just to make it more readable and logical. 只是为了使它更具可读性和逻辑性。

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

相关问题 带有新Rails应用程序的现有数据库 - Pre-existing database with new rails application 使用现有记录在Rails中向数据库添加新记录 - Using pre-existing records to add a new record to the database in rails Ruby on Rails:更新为破坏我模型的现有属性的宝石吗? - Ruby on Rails: Update to a gem clobbering my model's pre-existing attribute, options? Rails和预先存在的MySQL数据库:是否有简单的方法将数据库结构集成到Active Record中? - Rails & Pre-existing MySQL Database: Is there an easy way to integrate database structure into Active Record? Rails Active Storage 可以映射到数据库中预先存在的图像表吗? - Can Rails Active Storage map to a pre-existing Images table in the database? 通过systemd套接字激活来启动Ruby on Rails应用程序(即,启动Rails服务器监听预先存在的套接字) - Start Ruby on Rails app through systemd socket activation (i.e. start Rails server listening on pre-existing socket) Rails表单-检查预先存在的数据并填充字段 - Rails Form - Check for pre-existing data and populate fields Rails Select:当“选择”中没有“现有记录”时 - Rails Select: When Pre-existing Record is not in Select 使用现有数据库ID的Ruby on Rails无法正常工作 - Ruby on Rails using existing database ID not working 关联语法Ruby on Rails复数 - association syntax ruby on rails plural
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM