From: Andy Allan Date: Wed, 23 Feb 2022 15:15:07 +0000 (+0000) Subject: Specify inverse_of for trace tags and points X-Git-Tag: live~1820^2~1 X-Git-Url: https://git.openstreetmap.org./rails.git/commitdiff_plain/3cf3b3ce4e9551d532fee3b26c1f0a708cf53388 Specify inverse_of for trace tags and points Rails can't automatically detect bi-directional relationships when `:foreign_key` is present. Adding the `inverse_of` helps when validating traces and tags that haven't yet been saved. --- diff --git a/app/models/trace.rb b/app/models/trace.rb index bdafdd9d5..b9823ec90 100644 --- a/app/models/trace.rb +++ b/app/models/trace.rb @@ -31,8 +31,8 @@ class Trace < ApplicationRecord self.table_name = "gpx_files" belongs_to :user, :counter_cache => true - has_many :tags, :class_name => "Tracetag", :foreign_key => "gpx_id", :dependent => :delete_all - has_many :points, :class_name => "Tracepoint", :foreign_key => "gpx_id", :dependent => :delete_all + has_many :tags, :class_name => "Tracetag", :foreign_key => "gpx_id", :dependent => :delete_all, :inverse_of => "trace" + has_many :points, :class_name => "Tracepoint", :foreign_key => "gpx_id", :dependent => :delete_all, :inverse_of => "trace" scope :visible, -> { where(:visible => true) } scope :visible_to, ->(u) { visible.where("visibility IN ('public', 'identifiable') OR user_id = ?", u) }