开发者

Rails 2.3.9 pluralize model names issue

开发者 https://www.devze.com 2023-03-13 15:29 出处:网络
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).

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 %&g开发者_Python百科t;
<%=  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!


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


The easiest way is to do something like:

set_table_name "roles"

in your model.

0

精彩评论

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