如何防止Rails“多元化"?列名? [英] How can I prevent Rails from "pluralizing" a column name?

查看:85
本文介绍了如何防止Rails“多元化"?列名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将dwilkie的 foreigner 插件用于Rails.我有一个表创建语句,看起来像:

I'm using dwilkie's foreigner plugin for rails. I have a table creation statement that looks like:

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:

However, this generates the following SQL:

[4;35;1mSQL (2.7ms)[0m [0mCREATE TABLE "agents_games" ("agents_id" integer NOT NULL, "games_id" integer NOT NULL) [0m

我希望这些列被称为agent_idgame_id-而不是agents_idgames_id.如何防止Rails对列进行复数?

I want the columns to be called agent_id and game_id - not agents_id and games_id. How can I prevent Rails from pluralizing the columns?

我在enviornment.rb文件中尝试了以下操作,但没有帮助:

I tried the following in my enviornment.rb file, which didn't help:

ActiveSupport::Inflector.inflections do |inflect|
  inflect.uncountable "agent_id", "game_id"
end

推荐答案

找到了解决我问题的方法.我必须像这样将引用与引用分开声明外键:

Found the solution to my issue. I had to declare foreign keys separately from references like so:

create_table "agents_games", :force => true, :id => false do |t|
  t.references :agent
  t.foreign_key :agents,     :column => :agent_id, :null => false
  t.references :game
  t.foreign_key :games,      :column => :game_id, :null => false
end

有了这个,我可以拿出Inflector的东西了.

With this, I could take out the Inflector stuff.

这篇关于如何防止Rails“多元化"?列名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆