0

I have model Offer with many fields, among which are there are two fields that relate to the same model:

# == Schema Information
#
# Table name: offers
#
#  id                   :integer          not null, primary key
#  name                 :string(250)      default(""), not null
#  destination_id       :integer          not null
#  cruise_line_id       :integer          not null
#  ship_id              :integer          not null
#  departure_date       :date             not null
#  departure_port_id    :integer
#  arrival_date         :date             not null
#  arrival_port_id      :integer

departure_port_id and arrival_port_id relate to the same model Port, but can be also NULL, if no departure or arrival port provided.

How should Offer and Port models look like in this case?

Simone Carletti
  • 173,507
  • 49
  • 363
  • 364
kovpack
  • 4,905
  • 8
  • 38
  • 55

1 Answers1

0

Something like this:

class Offer < ActiveRecord::Base
  belongs_to :departure_port, class_name: "Port", foreign_key: "departure_port_id"
  belongs_to :arrival_port, class_name: "Port", foreign_key: "arrival_port_id"
end

class Port < ActiveRecord::Base
  has_one :offer
end
smallbutton
  • 3,377
  • 15
  • 27