Rails 2.3.9 复数模型名称问题

发布于 2024-11-16 03:47:11 字数 478 浏览 8 评论 0原文

我正在开发一个简单的选择。问题是我的代码需要使用西班牙语,所以 我必须使用 Rol (复数保持不变:角色),而不是使用角色。

所以,我的模型名称是:Rol,我的控制器名称是:Roles,在我的数据库中是:Roles

<% roles = Rol.all %>
<%=  collection_select(:usuario, :rol_id, roles, :id, :nombre, {:prompt => true}) %>

但是,当我运行该代码时,我得到:

ActiveRecord::JDBCError:表 “turaser2.rols”不存在:选择 *来自角色

(出于明显的原因)...所以,我的问题是:我如何告诉rails查看“roles”表而不是“rols”表?

谢谢你!

I'm developing a simple select. The thing is my code needs to be in spanish, so
instead of using Role I have to use Rol (the plurar remains the same: Roles).

So, my model name is: Rol and my Controller name: Roles and in my DB: Roles

<% roles = Rol.all %>
<%=  collection_select(:usuario, :rol_id, roles, :id, :nombre, {:prompt => true}) %>

However, when I run that code I get:

ActiveRecord::JDBCError: Table
'turaser2.rols' doesn't exist: SELECT
* FROM rols

(for obvious reasons)... So, my question is: how can I tell rails to look into the "roles" table instead the "rols" table?

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

書生途 2024-11-23 03:47:11

做到这一点的最佳方法是向变形器添加一个新条目。如果您仅使用 set_table_name 设置表名称,那么在您可能想要使用它的其他地方,复数形式仍然会失败。

转到 config/initializers/inflections.rb 并添加:

 ActiveSupport::Inflector.inflections do |inflect|
   inflect.irregular 'rol', 'roles'
 end

The best way to do this is to add a new entry to the inflector. If you only set the table name with set_table_name, the pluralization will still fail anywhere else you may want to use it.

Go to config/initializers/inflections.rb and add:

 ActiveSupport::Inflector.inflections do |inflect|
   inflect.irregular 'rol', 'roles'
 end
抱着落日 2024-11-23 03:47:11

执行以下操作:

set_table_name "roles"

最简单的方法是在您的模型中

The easiest way is to do something like:

set_table_name "roles"

in your model.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文