我正在使用 dwilkie 的 foreigner Rails 插件。我有一个表创建语句,如下所示:
create_table "agents_games", :force => true, :id => false do |t|
t.references :agents, :column => :agent_id, :foreign_key => true, :null => false
t.references :games, :column => :game_id, :foreign_key => true, :null => false
end
但是,这会生成以下 SQL:
[4;35;1mSQL (2.7ms)[0m [0mCREATE TABLE "agents_games"("agents_id"整数 NOT NULL, "games_id"整数 NOT NULL) [0m
我希望这些列被称为 agent_id
和 game_id
- 而不是 agents_id
和 games_id
。如何防止 Rails 使列复数?
我在我的 enviornment.rb
文件中尝试了以下操作,但没有帮助:
ActiveSupport::Inflector.inflections do |inflect|
inflect.uncountable "agent_id", "game_id"
end
最佳答案
一般来说,不要违背 ActiveRecord 的惯例,这是 AR 如此出色的原因之一。但是,如果对于这种一种情况,您想异常(exception),通过环境中的一些代码就很容易了。rb,请查看 http://api.rubyonrails.org/classes/Inflector/Inflections.html举个例子。
关于ruby-on-rails - 如何防止 Rails 的列名出现 "pluralizing"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2429516/