Alias for Model

I’m working on a Ticket model. The Ticket model needs to keep track of
the following:

  1. Requestor (the one that opens the ticket)
  2. Assignee (the one that gets assigned to work on it)
  3. Solver (the one that actually solved the ticket)

So the Ticket model should have this:

  1. requestor_id
  2. assignee_id
  3. solver_id

Now, I have a User model. The Requestor, the Assignee and the Solver are
all Users. So you can see how I CAN’T JUST ADD AN user_id attribute to
the Ticket model.

I have been searching for a model alias or something like that but can’t
seem to find it.

Can somebody please point me in the right direction?

Thanks,

Leonel

The requestor_id, assignee_id and solver_id in the tickets table should
all get their id’s from the users table. Still can’t figure it out. :-S

Thanks so much. Your reply did point me to the right information. This
is what I did…

User model
has_many :tickets , :foreign_key => ‘requestor_id’

Ticket model
belongs_to :user
belongs_to :requestor, :class_name => ‘User’, :foreign_key =>
‘requestor_id’

I haven’t setup the assignee or solver yet, but when the time comes,
I’ll know how to do it based on this.

Relationships - it’s all about relationships. Have you set them up
properly?

Here’s one way:

Models:

user, ticket

class Ticket < ActiveRecord::Base
belongs_to :user
belongs_to :requestor, :class_name => ‘User’
belongs_to :assignee, :class_name => ‘User’
belongs_to :solver, :class_name => ‘User’
end

class User < ActiveRecord::Base
has_many :tickets
has_many :requestors, :through => :tickets
has_many :assignees, :through => :tickets
has_many :solvers, :through => :tickets
end

ticket model should have:
user_id
requestor_id
assignee_id
solver_id

Open up a rails console and run a test to see what methods you now have:

User.new.methods.sort.each do |method|
p method
end

Ticket.new.methods.sort.each do |method|
p method
end

user = User.new
user.methods.sort.each do |method|
p method
end
user.public_methods.sort.each do |method|
p method
end

It’s always good to see what you get with relationships. You should be
able to do:

user = User.first
user.tickets
user.requestors
user.assignees
user.solvers