views:

66

answers:

2

Warning, I am new to ruby on rails. I know my database isn't setup all that great, but we're pulling in from a remote database and storing information from that database.

Users:
- id
- ...

stations
- id
- user_id
- hex_key (unique)
- ...

calls
- id
- reported by (hex key from stations)
- data source id (from remote database)

call details
- id
- call_index (data source id from calls)

responses
- id
- call_index (data source id from calls)
- response_id (from remote database)

response details
- id
- response_index (response_id from responses)

As far as the models go (this is all I have completed so far) I think this is also my biggest problem:

user has many stations, calls through stations and reports through calls
stations has many calls
calls has many responses and belongs to stations
response belongs to a call

I've been trying to figure this out, but how do i model this so i can get everything from the users correctly. something like this:
@user.responses.find(:all)
and that would give all the responses for that user

+2  A: 

here is some information on joining tables.

http://www.ruby-forum.com/topic/64839

James Parker
A: 

Based on the database schema and description here your models would look something like:

class User < ActiveRecord::Base has_many :stations end

class Station < ActiveRecord::Base belongs_to :user has_many :calls, :foreign_key => "reported by" end

class Call < ActiveRecord::Base has_many :call_details, :foreign_key => "call_index" end

The difficulty here is that the database schema is not exactly what Rails expects by default (the convention). You will also probably need to specify the primary key for the tables if they are not using the ID.

Also, I am not sure you will be able to construct @user.responses because responses belong to calls, and calls belong to stations, and stations belong to users. This is effectively @user.stations.calls.responses.

askegg