happened to me as well.
made it work by adding the join table as has_many to both models.
like this:
connection model:
module Alerts
class AlertIncidentConnection < ActiveRecord::Base
belongs_to :incident
belongs_to :alert
end
end
alert model:
module Alerts
class Alert < ActiveRecord::Base
has_many :alert_incident_connections, class_name: 'Alerts::AlertIncidentConnection'
has_many :incidents, through: :alert_incident_connections,class_name: 'Alerts::Incident', dependent: :destroy
end
end
incident model:
module Alerts
class Incident < ActiveRecord::Base
has_many :alert_incident_connections, class_name: 'Alerts::AlertIncidentConnection'
has_many :alerts, through: :alert_incident_connections,class_name: 'Alerts::Alert' ,dependent: :destroy
end
end
migration file:
class CreateTableAlertIncidentConnections < ActiveRecord::Migration
def change
create_table :alert_incident_connections do |t|
t.references :alert, null: false, index: true
t.references :incident, null: false, index: true
t.timestamps
end
end
end
usage:
alert.incidents << incident
alert.save!