Specify a table prefix for MySQL
Asked Answered
A

2

7

In CakePHP, I can specify a prefix in my database configuration, i.e. "so_", so that every model looks for its table under this prefix.

Is something similar possible in Rails, too? That is, can several programs share one database?

Assuming answered 20/4, 2010 at 10:13 Comment(0)
A
13

You might try the following in environments.rb: In the config section add the following code

config.active_record.table_name_prefix = "so_"
Azoth answered 20/4, 2010 at 11:6 Comment(2)
Works perfectly as of version 2.1.2.Assuming
It appears in 5.0 there is config/environments/development.rb, production.rb and test.rb.Raff
L
1

You can easily specify your own table name for each model with the set_table_name method:

class Mouse < ActiveRecord::Base
  set_table_name "so_mice"
end

But you have to do it for each model, I don´t know of any global configuration option.

Lorient answered 20/4, 2010 at 10:22 Comment(2)
Can I be sure that the database isn't dropped when I do fancy rake db-things?Assuming
As long as you don't do a "rake db:reset" or "rake db:migrate:reset" the database won't be dropped. Best way to prevent unintended database dropping is to not give the right to the user in question.Azoth

© 2022 - 2024 — McMap. All rights reserved.