I have two tables in two different schemas e.g.
cases
and events
.
In each schema I have table basic
events.basic
cases.basic
This tables have relations:
events.basic
has onecases.basic
(cases.basic
has 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.
:Events
and:Case
should probably be:events
and:case
but that probably won't fix it. – Negligible:Events
and:Case
looks like aliases and are not taken to SQL in this case – UltimoON "cases"."basic"."case_id" = "events"."basic"."case_id"
join condition look like? One of the tables doesn't have acase_id
and that's a problem, how would you write that join condition if you were doing it by hand? – Negligibleschema_search_path: public,events,cases
in yourdatabases.yaml
, then try yourrake db:schema:dump
. – Negligibleschema.rb
with everything in there but none of the table names include the PostgreSQL schema prefix, right? – Negligible