问题描述
我需要一张表,名字是校园。但铁路解释为复数,把一切都弄乱。
所以我做了一个,他们建议添加这样的变化:
I need a table whos name is Campus. But rails interprets that as plural, messing everything up.So I did a little research and they suggest to add an inflection like this:
ActiveSupport::Inflector.inflections do |inflect|
inflect.singular /^(campus)(es)?$/i, '\1'
inflect.plural /^(campus)$/i, '\1es'
end
我已将它添加到initializers / inflection.rb文件中。
我创建了一个新的迁移,删除表,并再次创建一个新的名为Campus,期望解决问题,但现在我不能得到的表Campus在控制台:
I have added this in the initializers/inflection.rb file. I created a new migration to drop the table and create a new one again called Campus, expecting to have the problem solved, but now I can't get the table Campus in the console:
$ rails console
Loading development environment (Rails 3.0.10)
1.9.2p290 :001 > Campus
=> Campus(Table doesn't exist)
我做错了什么?如何解决这个问题?
感谢您的帮助
What am I doing wrong? How can I fix this problem? Thanks for your help
我的校园模式是:
class Campus < ActiveRecord::Base
validates_presence_of :name
validates_presence_of :university_id
has_many :pois, dependent: :destroy
has_many :events, dependent: :destroy
has_many :market_items, dependent: :destroy
has_many :ads_points, dependent: :destroy
belongs_to :university
end
推荐答案
inflectors.rb
有一些标准代码,您的情况:
inflectors.rb
has some standard code that you can adjust for your situation:
# inflect.plural /^(ox)$/i, '\1en'
# inflect.singular /^(ox)en/i, '\1'
# inflect.irregular 'person', 'people'
所以你应该是:
inflect.plural /^(campus)$/i, '\1es'
inflect.singular /^(campus)es/i, '\1'
或者这可能更容易:
inflect.irregular 'campus', 'campuses'
对于这些偏转器,Rails将寻找一个名为校园
。由于您的表名为 campus
,因此您必须在 Campus
模型中添加以下内容:
With these inflectors Rails will be looking for a table called campuses
. Since your table name is campus
, you have to add the following to your Campus
model:
set_table_name 'campus'
在更改 inflectors.rb
后,务必重新启动服务器。
Be sure to restart your server after making changes to inflectors.rb
.
这篇关于表似乎失去了,当我试图定义一个'校园'模型在rails和设置复数名称自己的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!