Reputation: 3268
I have two classes with the following associations:
class Incident
has_one :assignee
has_one :technician
class User
has_many :incidents
Note that the assignee and technician fields refer to objects of type User. How should these relationships be in the model?
Upvotes: 4
Views: 816
Reputation: 4810
Here's a complete answer to this issue, in case people visiting this question are having a hard time putting everything together (as I was when I first looked into this).
Some parts of the answer take place in your Migrations and some in your Models:
class CreateIncidents < ActiveRecord::Migration
create_table :incidents do |t|
def up
t.references :assignee
t.references :technician
end
end
end
Here you are specifying that there are two columns in this table that will be referred to as :assignee and :technician and which hold references to another table. Rails will actually create columns called 'assignee_id' and 'technician_id' for you. In our case they will each reference rows in the Users table, but we specify that in the models, not in the migrations.
class Incident < ActiveRecord::Base
belongs_to :assignee, class_name => 'User'
belongs_to :technician, class_name => 'User'
end
Here you are creating a property on the Incident model named :assignee, then specifying that this property will be referencing an instance of the User class. Rails, seeing the 'belongs_to', will look for a column in your database called 'assignee_id', which we defined above, and use that to store the foreign key. Then you're doing the exact same thing for the technician.
This will allow you to access your assignee and technician, both instances of the User model, through an instance of the Incident model, like this:
Incident.assignee.name
Incident.technician.email
Here is your User Model:
class User < ActiveRecord::Base
has_many :assigned_incidents, :class_name => 'Incident', :foreign_key => 'assignee_id'
has_many :incidents_as_technician, :class_name => 'Incident', :foreign_key => 'technician_id'
end
Here you are creating a property on the User Model named :assigned_incidents, specifying that this property will be referencing instances of the Incident Model, and that the foreign key on the Incident model which references this, the User Model, that we want to use to for this property is called 'assignee_id'. Then you are doing the same thing for incidents_as_technician (this naming seems kind of awkward, but without better knowledge of what you're trying to do, I don't really have any great suggestions).
This allows you to get all of a user's assigned incidents or incidents as technician by doing this:
User.assigned_incidents
User.incidents_as_technician
Doing either of these will return an array of instances of the Incident model.
Upvotes: 1
Reputation: 916
Presumably the Incident should belong_to an assignee and technician, because the foreign key holding those relationships would be in the incidents table, not the employees table
class Incident belongs_to :assignee, :class_name => 'User' belongs_to :technician, :class_name => 'User' class User has_many :assigned_incidents, :class_name => 'Incident', :foreign_key => 'assignee_id' # not sure the wording you'd want to use for this relationship has_many :technician_incidents, :class_name => 'Incident', :foreign_key => 'technician_id'
You would want the foreign key fields to be incidents.assignee_id
, incidents.technician_id
Upvotes: 9