开发者

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

开发者 https://www.devze.com 2023-02-04 05:52 出处:网络
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 use

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, spe开发者_StackOverflow社区cs. At least that's what ruby on rails try's to lookup when I generate a scaffold .

How do I specifically state to use user instead of users in the sql. I don't see anywhere in my project where it is looking up the 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).

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.


You can specifiy the table name:

How do I explicitly specify a Model's table-name mapping in Rails?

0

精彩评论

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

关注公众号