问题
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
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
My campus model is:
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
回答1:
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'
So for you it should be:
inflect.plural /^(campus)$/i, '\1es'
inflect.singular /^(campus)es/i, '\1'
Or this maybe even easier:
inflect.irregular 'campus', 'campuses'
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'
Be sure to restart your server after making changes to inflectors.rb
.
来源:https://stackoverflow.com/questions/11943432/table-seems-lost-when-i-try-to-i-define-a-campus-model-in-rails-and-set-the-p