简体   繁体   中英

Rails associations with the same models

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?

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

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:

Migrations

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.

Models

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM