11

I looked around on Stack Overflow and Agile Development with Rails but couldn't find anything that answered all the parts of this I need.

I just generated a Cow model in rails. Apparently, Rails uses an antiquated plural of cow ("kine"), so when I created that model, it built a Kine migration:

class CreateKine < ActiveRecord::Migration
  def change
    create_table :kine do |t|
      t.string :name
      t.string :farm
      t.string :breed

      t.timestamps
    end
  end
end

I know I could go into the model's .rb file and set_table_name back to cow, but I'm worried about associated controllers. If I create a Cows controller, will it not sync up?

How do I get everything to be Cow/Cows?

This is one of my first apps, and I'm already way confused by managing controller-model associations, so this inflection issue doesn't help.

2
  • 4
    This rather arcane irregular inflection was removed from Rails a few days ago. Future versions will properly inflect cow as cows. Aug 14, 2013 at 3:12
  • 3
    Good to hear. Though that's going to suck for everyone who wants to write a Rails 4 app to optimize 14th century Anglo-Saxon cattle herding.
    – Sasha
    Aug 14, 2013 at 7:23

2 Answers 2

26

Create an inflection here is an example:

config>initializers>inflections.rb

ActiveSupport::Inflector.inflections do |inflect|
  inflect.irregular 'cow', 'cows'
end
4
  • Great. And then just run rake db:migrate again?
    – Sasha
    Sep 19, 2012 at 5:36
  • Or should I roll back the model and then generate it again?
    – Sasha
    Sep 19, 2012 at 5:39
  • 3
    This is the right answer. @Sasha, just delete the migration and re-run the generator; it should generate Cows this time. Aug 13, 2013 at 19:43
  • @Sasha just destroy what you generated with: rails destroy model Cow and then run the generator after adding the code mentioned in this answer
    – grant
    May 20, 2015 at 4:32
2

Try to rename the table:

class RenameKineToCows< ActiveRecord:Migration

  def up
    rename_table :kine, :cows
  end 
  def down
    rename_table :cows, :kine
  end
end

Rename your app/models/kine.rb to cow.rb and edit the file

class Cow < ActiveRecord::Base

  self.table_name = 'Cow'
end

Rename your app/controllers/kine_controller.rb to cows_controller.rb and edit the file

class KineController < ApplicationController 

to

class CowsController < ApplicationController

and edit config/routes.rb

resources :kine

to

resources :cows
1
  • Thanks so much! Just double checking. In the migration you wrote up top, should self.up and self.down be the same? Or should self.down be "rename_table :cows, :kine"?
    – Sasha
    Sep 19, 2012 at 6:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.