我使用dwilkie的外国人插件导轨。我有一个表的创建声明如下:

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" integer NOT NULL, "games_id" integer NOT NULL) [0m

我想要的列被称为agent_idgame_id - 不agents_idgames_id。如何防止滑轨从复数化列?


我想在我的enviornment.rb文件下面,这并没有帮助:

ActiveSupport::Inflector.inflections do |inflect|
  inflect.uncountable "agent_id", "game_id"
end
有帮助吗?

解决方案 2

找到了解决我的问题。我不得不像这样引用单独声明外键:

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

通过此,我可以取出变形器的东西。

其他提示

在一般情况下,不打的ActiveRecord的约定,这是什么使AR工作这么好一部分。然而,如果此的一个案例的你想一个例外,它是通过在你的environment.rb一些代码很容易,检查出的 http://api.rubyonrails.org/classes/Inflector/Inflections.html

我想你会得到你想要的东西,如果你在引用中使用的单一型号名称,像这样的:

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

这是更明确的方式,反映了你的连接表中的每一行将具有一个AGENT_ID和一个game_id,从而将引用一个试剂与一个游戏。

在附加拐点或外键声明就没有必要在这种情况下。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top