classCreateDemographyLanguages<ActiveRecord::Migrationdefchange# Create table `languages` in schema `demography`create_table"languages",:schema=>"demography"do|t|t.string:namet.string:code,:limit=>2end# Add PostgreSQL commentsset_table_comment"demography.languages","List of languages"set_column_comments"demography.languages",:name=>"Full name of language in English",:code=>"ISO 639-1 code"endend
db/migrate/create_demography_countries.rb
1234567891011121314151617181920
classCreateDemographyContries<ActiveRecord::Migrationdefchange# Create table `countries` in schema `demography`create_table"countries",:schema=>"demography"do|t|t.string:name# In real life you likely would have many-to-many associatont.integer:language_idend# Add PostgreSQL commentsset_table_comment"demography.countries","List of world countries"set_column_comments"demography.languages",:name=>"Full name of country in English",:language_id=>"Most popular language in the country"# Add foreign key and create index on demography.countries.language_idadd_foreign_key("demography.countries","demography.languages")endend
Great! Now we need to set table names in models to make ActiveRecord know that
these tables are located in demography schema.
It will work. But I would recommend you to create module Demography which would represent
demography schema and move those models to it. One more benefit is that you can define
schema prefix in module and models will use it build table name automatically.