Rails 모델에 여러 PostgreSQL 스키마 사용
Rails 애플리케이션을위한 PostgreSQL 데이터베이스가 있습니다. 'public'이라는 스키마에서 주요 Rails 모델 테이블이 저장됩니다. 저는 'public'스키마와 이름이 같은 테이블을 갖는 'discogs'스키마를 만들었습니다. 이것이 그 이유 중 하나입니다. 나는 이것을 구성하기 위해 스키마를 사용하고 있습니다.
내 앱의 '디스코 그'스키마에서 모델을 어떻게 설정합니까? 저는 Sunspot을 사용하여 Solr이 이러한 모델을 색인화하도록 할 것입니다. 어떻게할지 잘 모르겠습니다.
database.yml의 PostgreSQL 어댑터 schema_search_path가 문제를 해결합니까?
development:
adapter: postgresql
encoding: utf-8
database: solidus
host: 127.0.0.1
port: 5432
username: postgres
password: postgres
schema_search_path: "discogs,public"
또는 각 스키마에 대해 서로 다른 연결을 지정할 수 있습니다.
public_schema:
adapter: postgresql
encoding: utf-8
database: solidus
host: 127.0.0.1
port: 5432
username: postgres
password: postgres
schema_search_path: "public"
discogs_schema:
adapter: postgresql
encoding: utf-8
database: solidus
host: 127.0.0.1
port: 5432
username: postgres
password: postgres
schema_search_path: "discogs"
각 연결을 정의한 후 두 모델을 만듭니다.
class PublicSchema < ActiveRecord::Base
self.abstract_class = true
establish_connection :public_schema
end
class DiscoGsSchema < ActiveRecord::Base
self.abstract_class = true
establish_connection :discogs_schema
end
그리고 모든 모델은 각 스키마에서 상속됩니다.
class MyModelFromPublic < PublicSchema
set_table_name :my_table_name
end
class MyOtherModelFromDiscoGs < DiscoGsSchema
set_table_name :disco
end
도움이되기를 바랍니다.
The correct one for rails 4.2 is as:
class Foo < ActiveRecord::Base
self.table_name = 'myschema.foo'
end
More info -http://api.rubyonrails.org/classes/ActiveRecord/ModelSchema/ClassMethods.html#method-i-table_name-3D
Just do
class Foo < ActiveRecord::Base
self.table_name = 'myschema.foo'
end
In migrations:
class CreateUsers < ActiveRecord::Migration
def up
execute 'CREATE SCHEMA settings'
create_table 'settings.users' do |t|
t.string :username
t.string :email
t.string :password
t.timestamps null: false
end
end
def down
drop_table 'settings.users'
execute 'DROP SCHEMA settings'
end
end
Optional in model
class User < ActiveRecord::Base
self.table_name 'settings.users'
end
Because set_table_name
was removed, and it was replaced by self.table_name
.
I think you should code follow as:
class Foo < ActiveRecord::Base
self.table_name = 'myschema.foo'
end
method set_table_name
has been remove. self.table_name
works fine.
참고URL : https://stackoverflow.com/questions/8806284/using-multiple-postgresql-schemas-with-rails-models
'developer tip' 카테고리의 다른 글
사악한 정규식을 어떻게 인식 할 수 있습니까? (0) | 2020.11.21 |
---|---|
Visual Studio 2012 64 비트? (0) | 2020.11.21 |
특정 매개 변수가있는 robot.txt의 URL을 무시 하시겠습니까? (0) | 2020.11.20 |
mercator 투영에서 위도 / 경도 지점을 픽셀 (x, y)로 변환 (0) | 2020.11.20 |
클래스 A는 여러 JSON 필드를 선언합니다. (0) | 2020.11.20 |