0

I have Employee model and TicketStatus model using STI approach on Employee model in order to have to child models 'Advisor' and 'Staff', inside my TicketStatus i am trying to achieve the following: making a relation between TicketStatus and Advisor , TicketStatus and Staff, then TicketStatus and PreviousAdvisor , TicketStatus and PreviousStaff, my code is like this:

class TicketStatus < ActiveRecord::Base
  attr_accessible :status_id, :ticket_id , :staff_id, :advisor_id, :previous_advisor_id, :previous_staff_id
  belongs_to :status
  belongs_to :ticket
  belongs_to :staff, class_name: 'Staff', foreign_key: 'staff_id'
  belongs_to :previous_staff , class_name: 'Staff', foreign_key: 'previous_staff_id'
  belongs_to :advisor, class_name: 'Advisor', foreign_key: 'advisor_id'
  belongs_to :previous_advisor, class_name: 'Advisor', foreign_key: 'previous_advisor_id'
end

enter image description here

the problem is i cannot have two association on the same class name , after generating a dependency diagram using rubymine i found that the second association is overwriting the first association , what can i do in order to make those association works correctly ?

Holger Just
  • 52,918
  • 14
  • 115
  • 123
Mostafa Hussein
  • 11,063
  • 3
  • 36
  • 61

1 Answers1

0

This is happening because the foreign keys are the same. You can specify what the foreign keys should be for the second relationship in both cases like this:

belongs_to :previous_staff , class_name: 'Staff', foreign_key: 'previous_staff_id'

belongs_to :previous_advisor, class_name: 'Advisor', foreign_key: 'previous_advisor_id'

That'll preserve the separate relationships. The staff and the advisor relationships are fine.

EDIT: This was answered after the considerable edit of the question.

Niall Paterson
  • 3,580
  • 3
  • 29
  • 37
  • i have updated my question , i have specified foreing key for each association and still have the same problem , i have added a screen shot from the diagram – Mostafa Hussein Dec 17 '13 at 12:43
  • I didn't change anything, but I assume you actually tried this out by running `rails c` or are you just going by what ruby mine is saying? Doesn't – Niall Paterson Dec 17 '13 at 17:37
  • i have added the foreign keys then generated a diagram it still overwrite the association – Mostafa Hussein Dec 17 '13 at 17:47
  • yeah but like you can't be 100% certain ruby mine is wrong. It could be a bug, why not run it and try it out in the console? – Niall Paterson Dec 17 '13 at 18:35