Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incorrect Pluralizing of Model in Rails

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? Thanks. 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.

like image 469
Sasha Avatar asked Sep 19 '12 04:09

Sasha


2 Answers

Create an inflection here is an example:

config>initializers>inflections.rb

ActiveSupport::Inflector.inflections do |inflect|
  inflect.irregular 'cow', 'cows'
end
like image 100
Rodrigo Zurek Avatar answered Sep 19 '22 06:09

Rodrigo Zurek


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
like image 41
Claud Kho Avatar answered Sep 19 '22 06:09

Claud Kho