2012-05-07 5 views
0

ActiveRecord에서 데이터베이스를 만드는 방법을 찾지 못했습니다. 저는 이것을 보았습니다 : http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/PostgreSQLAdapter.html#method-i-create_database,하지만 놀고 싶지는 않습니다. 내가 말할 수있는 한 ... 나는 내가 무엇을 놓치고 있는지 확실하지 않습니다. 이것은 가능한가?ActiveRecord - 레일을 사용하지 않고 데이터베이스 만들기

감사합니다.

+0

이 질문은 Rails가 없으면 나왔지만 Rails 페이지를 인용합니다. 당신이 시도한 바를 잘 설명하지 못하거나 "훌륭하게 놀고 싶지 않습니다."라는 말의 의미를 설명하지 않습니다. – kgrittn

+0

[Goliath] (https://github.com/postrank-labs/goliath/) 및 ActiveRecord를 사용하고 있습니다. 내가 무슨 일이 일어나고 있는지 정말로 알지 못했기 때문에 그 당시에는 더 구체적 일 수 없었다. 전에 너무 짧게 사과드립니다. – wulftone

답변

4

영웅과 내 로컬 컴퓨터에서 모두 작동하는이 항목을 함께 처리하여 하나가 존재하지 않는 경우 데이터베이스를 만듭니다.

# ./lib/db_connect.rb 

require 'erb' 
require 'uri' 
require 'em-synchrony/activerecord' 

class DbConnect 
    attr_accessor :config 
    def initialize 
    @db = URI.parse(ENV['DATABASE_URL'] || 'http://localhost') 
    if @db.scheme == 'postgres' # This section makes Heroku work 
     ActiveRecord::Base.establish_connection(
     :adapter => @db.scheme == 'postgres' ? 'postgresql' : @db.scheme, 
     :host  => @db.host, 
     :username => @db.user, 
     :password => @db.password, 
     :database => @db.path[1..-1], 
     :encoding => 'utf8' 
    ) 
    else # And this is for my local environment 
     environment = ENV['DATABASE_URL'] ? 'production' : 'development' 
     @db = YAML.load(ERB.new(File.read('config/database.yml')).result)[environment] 
     ActiveRecord::Base.establish_connection(@db) 
     @config = ActiveRecord::Base.connection.pool.spec.config 
    end 
    end 
end 

# connect to the database 
DbConnect.new 

다음은이 파일을 사용하는 Rakefile입니다.

# ./Rakefile 

$: << './lib' 
require 'db_connect' 

db = DbConnect.new 
config = db.config 

desc 'create the database' 
task :create_db do 
    unless ENV['DATABASE_URL'].present? 
    ActiveRecord::Base.establish_connection adapter:'postgresql',username:config[:username],password:config[:password], database: 'postgres' 
    ActiveRecord::Base.connection.create_database config[:database] 
    else 
    raise StandardError, "You cannot do this on Heroku!" 
    end 
end 

desc 'create the users table' 
task :create_users_table do 
    ActiveRecord::Schema.define(:version => 0) do 
    create_table "users", :force => true do |t| 
     t.string "first_name" 
     t.string "last_name" 
     t.datetime "created_at", :null => false 
     t.datetime "updated_at", :null => false 
    end 
    end 
end 

문제가 해결되었습니다.

관련 문제