Trouble with relationships

Hi, I'm a bit rusty with this, but I'm trying to get some relationship stuff working with an existing application and legacy database that I have been working with. Here's the way it works:

I have time_accounting which can belong to tickets and tickets belong to queues. Ultimately, I am trying to get a list of all the time_accounting tickets that exist under a queue. I have the following models:

class TicketQueue < ActiveRecord::Base         def self.table_name() "queue" end         has_many :ticket, :foreign_key => '$queue_id' end class Ticket < ActiveRecord::Base         def self.table_name() "ticket" end         has_many :time_accounting, :foreign_key => '$ticket_id'         belongs_to :ticket_queue, :foreign_key => '$queue_id' end class TimeAccounting < ActiveRecord::Base         def self.table_name() "time_accounting" end         belongs_to :ticket, :foreign_key=> "ticket_id"         belongs_to :article, :foreign_key=> "article_id" end

The belongs_to relationship between time accounting and tickets seems to work, but I can't get the one for queues and their tickets to work, which I need to derive the time accounting data for the tickets in each queue.

If I do something like this, it just hangs:

my_queue = TicketQueue.find 15

=> #<TicketQueue:0x2acc0f2db330 @attributes={"lock_notify"=>"0", "system_address_id"=>"1", "name"=>"interlink::interlink-internal-support", "valid_id"=>"1", "state_notify"=>"0", "follow_up_lock"=>"0", "escalation_time"=>"0", "comments"=>"", "salutation_id"=>"1", "create_by"=>"2", "create_time"=>"2006-08-28 14:13:53", "move_notify"=>"0", "group_id"=>"5", "id"=>"15", "change_by"=>"2", "change_time"=>"2006-08-28 14:13:53", "signature_id"=>"1", "unlock_timeout"=>"0", "default_sign_key"=>"", "owner_notify"=>"0", "follow_up_id"=>"1"}>

my_queue.ticket

Does anyone see anything obvious that I am screwing up here? I can do something like:

test = TimeAccounting.find 16

=> #<TimeAccounting:0x2acc0e7f1c98 @attributes={"time_unit"=>"0.25", "create_by"=>"2", "create_time"=>"2006-09-01 08:29:07", "id"=>"16", "change_by"=>"2", "change_time"=>"2006-09-01 08:29:07", "ticket_id"=>"21", "article_id"=>"73"}>

test.ticket.queue_id

=> 1

So the time_accounting relationship with tickets works. Although I am not sure it works in reverse, as I get a similar hang if I pull up a specific ticket, and then try to get the time_accounting information from it.

Thanks, Leah

Leah R. M. Cunningham wrote:

Hi, I'm a bit rusty with this, but I'm trying to get some relationship stuff working with an existing application and legacy database that I have been working with. Here's the way it works:

I have time_accounting which can belong to tickets and tickets belong to queues. Ultimately, I am trying to get a list of all the time_accounting tickets that exist under a queue. I have the following models:

class TicketQueue < ActiveRecord::Base          set_table_name 'queue'          has_many :tickets, :foreign_key => '$queue_id' end

class Ticket < ActiveRecord::Base          has_many :time_accountings, :foreign_key => '$ticket_id'          belongs_to :ticket_queue, :foreign_key => '$queue_id' end

class TimeAccounting < ActiveRecord::Base          belongs_to :ticket          belongs_to :article end

When you use has_many, the symbol should be written with plural.

set_table_name will be handy for you but you only need it when the model name cannot be used to infer the table name.

Better to use single quotation marks than double in your cases since you dont use #{} inside any of them. Faster Ruby this way as it doesn't have to look inside the string.

I assume the dollar signs are part of the legacy stuff. I've never seen anything like that before. However these need to match

           has_many :time_accountings, :foreign_key => '$ticket_id'            belongs_to :ticket, :foreign_key=>'$ticket_id'

           OR if the column name doesn't really have the dollar sign then

           has_many :time_accountings            belongs_to :ticket

Could you name your TicketQueue model just Queue? This will match the legacy table name better. Less deviations from the Rails assumptions is better.

If you have a complete hang make sure you look at the bottom of log/development.log to get any hints.

Peter

Peter,

Thanks for the suggestions. I will give those a shot to clean up the code and see if it helps. Will update soon with a more detailed reply, but in regards to:

petermichaux@gmail.com wrote:

Leah R. M. Cunningham wrote Could you name your TicketQueue model just Queue? This will match the legacy table name better. Less deviations from the Rails assumptions is better.

I tried initially naming the TicketQueue model Queue, but it failed to work as a model as it is a Rails keyword, so I had to name it something else. See http://wiki.rubyonrails.org/rails/pages/ReservedWords

Cheers, Leah

Fair enough

normally table names are plural so unless this is a legacy issue you might want

set_table_name 'queues'

Peter