Skip to main content

database.yml rails

database.yml rails ?

Database.yml is one of the most important files in rails project, database.yml rails is actualy a configuration file.
What is Contains?

Database.yml is configuration file which tells rails about database, database information such as
1. database host/machine
2. which database to choose ( mySQL, Oracle, Sqlite... etc)
3. Username
4. password
.
There are such multiple configurations here in this file.
You need to write appropriate values in this configuration based on the Database you are using, and host/machine of the DB. If you want to use

MySQL Database.yml :

development:
adapter: mysql2
encoding: utf8
reconnect: true
database: [DB name]
pool: 5
username: [your username]
password: [your password]
host: [host name]

Now If you are a java programmer, you might land up with a question in your mind that what about the drivers/connectors required. In java we need JAR files depending upon the database we are using.Rails will automatically detect the drivers based on the adapter parameter in the yml file and connect appropriately. You just need to provide appropriate Jem which is equivalent to a Jar file.

With the above yml file your project is good to proceed.
Similarly you need to change some configurations to connect to some other database, such as Oracle..

Oracle Database.yml:

adapter: oracle
database: comics_catalog_development
username: [your username]
password: [your password]
host: [host name]

postgres Database.yml:

development:
adapter: postgresql
encoding: unicode
database: [DB name]
pool: 5
username: [your username]
password: [your username]

IBMDB2 Database.yml:

development:
adapter: jdbc
driver: com.ibm.db2.jcc.DB2Driver
url: jdbc:db2://HOSTNAME:50000/
host: localhost
port: 50000
database: [DB name]
username: [your username]
password: [your password]

MSSQL Database.yml:

development:
adapter: sqlserver
mode: ODBC
username: [your username]
password: [your password]
database: [DB name]
dsn: my_dev_server_ds

Rails has capability to connect to majority of the databases present today.
You can also create database.yml with various environments such as you need some different database for
development,testing,production as :

development:

adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000

test:

adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000

production:

adapter: mysql
encoding: utf8
username: [your username]
password: [your password]
database: [DB name]
socket: /tmp/mysql.sock
host: [host machine]    
port: 3306

Comments

Popular posts from this blog

Get the Current url in ruby on rails

request.refererrequest.fullpathrequest.fullpath.split("?")[0]request.referer or request.env['HTTP_REFERER'] your controller to get the referer url. 

--------------------
request.original_url
request.url
request.host_with_port
<link rel="canonical" href="<%= request.url %>" />
<%=request.path%>
How can I get the current absolute URL in my Ruby on Rails view?
The request.request_uri only returns the relative URL.
request.env['REQUEST_URI']

 For Rails 3.2 or Rails 4 you should use request.original_url to get the current URL. More detail.

For Rails 3: You want "#{request.protocol}#{request.host_with_port}#{request.fullpath}", since request.url is now deprecated.

For Rails 2: You want request.url instead of request.request_uri. This combines the protocol (usually http://) with the host, and request_uri to give you the full address

Gem::LoadError (Specified 'mysql2' for database adapter, but the gem is not loaded. Add `gem 'mysql2'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord).?

Gem::LoadError (Specified 'mysql2' for database adapter, but the gem is not loaded. Add `gem 'mysql2'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord).) In rails?


1)change the gemfile in mysql version
 gem 'mysql2'

2)Change to mysql2 version
 gem 'mysql2','0.3.20'

Active Scaffold : Add radio buttons to your form ruby on rails ?

Active Scaffold : Add radio buttons to your form ruby on rails ?

The way to add radio buttons to your active scaffold create/update from is as shown below

#Controller

class UsersController < ApplicationController
active_scaffold :user do |config|
config.label = “Users”
config.columns = [:name, :gender]
config.columns[:gender].form_ui = :radio
config.columns[:gender].options[:options] = [['Male', '1'], ['Female','2']]
end
end