I am not an experienced Rails developer, developing a Rails v2.3 application with MySQl v5.1 .
I am not sure how to achieve the following thing:
I need to create:
1. A new environment (that's a new environment besides development, production and test environments) named 'special' environment
2. A new database environment for above created special environment , what I did is to add the following thing in config/database.yml
special:
adapter: mysql2
host: localhost
username:
database: special_db
encoding: latin1
3. A rake task to run under the special environment and the code in the rake task only deal with the special database.
To achieve this, I know I need to define some configurations, but not sure:
What/How exactly are needed to configure to create the special environment & database?
(As you saw above, I only defined the special database in database.yml file, but where & how to define and configuration for the new enverionment?)
How to run the rake task code under the special environment and only deal with the special database in Rails. Could someone please
P.S.
I need to run everything in a rake task not from command line. How to change environment and how to check the change?
--------------Found reason, but not sure the solution---------------------
Ok, I found the reason of this wired problem is because of the mysql2
gem which seems can not load the new "special" environment, if I switch to use mysql
gem , the problem will be gone. But this probject has to use mysql2 for some other reason. How to get rid of this mysql2
problem?
By default rails has 3 environments, development , production and test . By editing each file you are editing the configuration for that environment only. Rails also has a configuration file in config/application.
You cannot switch environments once Rails has been loaded in a process (for server, console, tests, rake tasks,...). You need to specify the environment when launching the process, and cannot change it afterwards. Stop the process, and start again with another environment if you need it.
When we generate a new Rails application we get three environments by default: development , test and production . There's nothing special about these particular environments, though, and there are very few places in the Rails source code that refer to them.
Location: specify a path to the directory in which you want to create the project. By default, RubyMine creates a directory with the same name as the project. Ruby SDK: select a required Ruby interpreter installed on your system. Rails version: select a Rails version that yoou want to use in your project.
Try the following:
Copy the config/environments/development.rb to config/environments/special.rb
Create the database using
$ RAILS_ENV=special rake db:create
$ RAILS_ENV=special rake db:migrate
$ RAILS_ENV=special rails s
Put this into your rake task:
RAILS_ENV = 'special'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With