2011-09-14 2 views
5

내가 ROR application.My의 database.yml을 여러 데이터베이스에 연결을 시도하고 당신의 database.yml을 파일에서이 같이여러 DB 연결

개발이다

adapter: mysql 
username: root 
password: 
database: example_private_development 

이 establish_connection하여 접속할 수있다 : 비공개

내 의심스러워하는 방법은 rake db : create.I Google에서 해결책을 얻을 수 없다는 것입니다.

삭제 해 주시면 감사하겠습니다.

답변

6

rake db:create:all 

을 시도 그리고 네, 그것은 레일 응용 프로그램에서 여러 DB 연결이 가능합니다.

이것은 내가 한 번했기 때문에 ActiveRecord::Base에서 상속받은 두 개의 클래스를 만들고이 클래스 내부의 연결을 설정합니다.

그럼 내가 대신 아래 직접 ActiveRecord

의 이러한 클래스 중 하나에 내 모든 모델을 상속 한 예입니다

: 그럼 모두 TEST1 및 TEST2 데이터베이스에 대한 두 가지 모델이

database.yml file 

#app uses two database 
#1 - test1 
#2 - test2 
test1: 
    adapter: mysql 
    encoding: utf8 
    database: test1 
    username: root 
    password: xxx 
    host: localhost 

test2: 
    adapter: mysql 
    encoding: utf8 
    database: test2 
    username: root 
    password: xxx 
    host: localhost 

class Test1Base < ActiveRecord::Base 
    self.abstract_class = true 
    establish_connection("test1") 
end 

class Test2Base < ActiveRecord::Base 
    # No corresponding table in the DB. 
    self.abstract_class = true 
    establish_connection("test2") 
end 

다음 데이터베이스에 따라 모델을 상속받습니다.

class School < Test1Base 
    #code 
end 

class Student < Test2Base 
    #code 
end 
+0

안녕 사 미라, 당신의 대답은 올바른 것입니다. 나는 "portal_development"에 대한 모델을 마이그레이션 할 수있는 또 하나의 질문이 있습니다. 도와주세요 ... –

+0

안녕하세요 @Shamith, 오ops, 그 오타되었습니다, 그 env 이름이 있어야합니다, 위의 database.yml을 편집했습니다, 예를 들어 내 기존 파일을 삭제하는 것을 잊어 버렸습니다. 죄송합니다 ... : D – sameera207

2

답장을 보내 주셔서 감사합니다.

우리는 예를

db:migrate RAILS_ENV="portal_development"'를 들어, 특정 DB에 대한 모델을 마이그레이션 할 수 있습니다. DB.check와 연결을 설정

그리고 더 변화는 여러분의 소중한 응답을

class Test1Base < ActiveRecord::Base 
    self.abstract_class = true 
    establish_connection :development 
end 

class Test2Base < ActiveRecord::Base 
    # No corresponding table in the DB. 
    self.abstract_class = true 
    establish_connection :portal_development 
end 

감사의 사 미라 아래 수정.

환호

Shamith의 C