I have two tables in two different schemas e.g.
cases and events.
In each schema I have table basic
events.basiccases.basic
This tables have relations:
events.basichas onecases.basic(cases.basichas manyevents.basic)
My attempts have failed:
file cases_basic.rb
class CasesBasic < ActiveRecord::Base
set_table_name 'cases.basic'
set_primary_key 'case_id'
has_many :Events, :class_name => 'EventsBasic', :foreign_key => 'case_id'
end
file events_basic.rb
class EventsBasic < ActiveRecord::Base
set_table_name 'events.basic'
set_primary_key 'event_id'
belongs_to :Case, :class_name => 'CasesBasic', :foreign_key => 'case_id'
end
Enviroment:
Ruby 1.9.3, Rails 3.1.3, gem 'pg'
I Need answer for this questions:
- how to handle this situation in Rails Active Record?
- how to query this tables?
- how to handle this situation in
rake db:schema:dump
EDIT:
After changing belongs_to and has_many (like Catcall suggest) i have the same error
PGError: ERROR: column basic.case_id does not exist
LINE 1: ...IN "cases"."basic" ON "cases"."basic"."case_id" = "events"."...
^
: SELECT "events"."basic".* FROM "events"."basic" INNER JOIN "cases"."basic" ON "cases"."basic"."case_id" = "events"."basic"."case_id" LIMIT 3
Rails generate bad SQL. I should be done using some aliases:
SELECT t1.* FROM "events"."basic" t1 INNER JOIN "cases"."basic" t2 ON t1."case_id" = t2."case_id" LIMIT 3
EDIT 2: Ok It was my f*** bug, i didn't add events.basic.case_id column and foreign key in my example database. It works!
Questions 1 AND 2 are working but we have question about rake db:schema:dump what about it? Rails generates models only for public schema.
I have so many tables and relations that i want to generate them.
:Eventsand:Caseshould probably be:eventsand:casebut that probably won't fix it.:Eventsand:Caselooks like aliases and are not taken to SQL in this caseON "cases"."basic"."case_id" = "events"."basic"."case_id"join condition look like? One of the tables doesn't have acase_idand that's a problem, how would you write that join condition if you were doing it by hand?schema_search_path: public,events,casesin yourdatabases.yaml, then try yourrake db:schema:dump.schema.rbwith everything in there but none of the table names include the PostgreSQL schema prefix, right?