Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a new environment in Rails

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:

  1. 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?)

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

like image 918
Mellon Avatar asked Nov 25 '11 08:11

Mellon


People also ask

What are environments in Rails?

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.

How do I change environment in Ruby on Rails?

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.

What environments Does Rails have by default?

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.

How do I create a new project in Ruby on Rails?

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.


2 Answers

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
like image 62
map7 Avatar answered Oct 20 '22 02:10

map7


Put this into your rake task:

RAILS_ENV = 'special'
like image 23
auralbee Avatar answered Oct 20 '22 03:10

auralbee