Forum: Ruby on Rails Database names

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ead43bcb0202410540a6a7cee5955d46?d=identicon&s=25 Fresh Mix (giga)
on 2009-01-28 21:44
If I generate a model by: "./script generate model user" it make a table
"users".
But if I want that table is "user" not "users" what I can do?
280b78a61a968391b7e07e912be102a8?d=identicon&s=25 Robert Walker (robert4723)
on 2009-01-28 22:48
James Bond wrote:
> If I generate a model by: "./script generate model user" it make a table
> "users".
> But if I want that table is "user" not "users" what I can do?

You can override the default table name in your migration file after
running the generator.

However, if I had anything to say about it I would not allow a developer
to break this Rails convention. The only allowable exception would be in
the case of a pre-existing database schema that is being used somewhere
outside the Rails project.

But, if you want to go to all the extra trouble to break with
convention, Rails will certainly let you do so.

You need to change the table name in the migration and also set the
table name in the model so it knows what to look for:

Example:
-------------------
class Project < ActiveRecord::Base
  set_table_name "project"
end

class CreateProjects < ActiveRecord::Migration
  def self.up
    create_table :project do |t| # Note singular form
      t.string :name
      t.text :description

      t.timestamps
    end
  end

  def self.down
    drop_table :project # And here too
  end
end
85f8b7bbe07bff22e0b38cca818cf692?d=identicon&s=25 Luke Pearce (kule)
on 2009-01-28 22:50
James Bond wrote:
> If I generate a model by: "./script generate model user" it make a table
> "users".
> But if I want that table is "user" not "users" what I can do?

Think this is what you're looking for:

http://wiki.rubyonrails.org/rails/pages/howtousele...

Cheers
Luke
This topic is locked and can not be replied to.