]> git.openstreetmap.org Git - rails.git/blobdiff - app/models/relation.rb
Use association names with :joins instead of writing the SQL for the
[rails.git] / app / models / relation.rb
index 37969ee76bf5912f7ac47feec84b6b4ad5ddf679..09143294ceb48fab466f9780a5f34fb7706d6daf 100644 (file)
@@ -242,8 +242,9 @@ class Relation < ActiveRecord::Base
     Relation.transaction do
       check_consistency(self, new_relation, user)
       # This will check to see if this relation is used by another relation
-      rel = RelationMember.find(:first, :joins => "INNER JOIN current_relations ON current_relations.id=current_relation_members.id", :conditions => [ "visible = ? AND member_type='Relation' and member_id=? ", true, self.id ])
-      raise OSM::APIPreconditionFailedError.new("The relation #{new_relation.id} is a used in relation #{rel.id}.") unless rel.nil?
+      rel = RelationMember.find(:first, :joins => :relation, 
+                                :conditions => [ "visible = ? AND member_type='Relation' and member_id=? ", true, self.id ])
+      raise OSM::APIPreconditionFailedError.new("The relation #{new_relation.id} is used in relation #{rel.relation.id}.") unless rel.nil?
 
       self.changeset_id = new_relation.changeset_id
       self.tags = {}
@@ -318,12 +319,12 @@ class Relation < ActiveRecord::Base
   # if any members are referenced by placeholder IDs (i.e: negative) then
   # this calling this method will fix them using the map from placeholders 
   # to IDs +id_map+. 
-  def fix_placeholders!(id_map)
+  def fix_placeholders!(id_map, placeholder_id = nil)
     self.members.map! do |type, id, role|
       old_id = id.to_i
       if old_id < 0
         new_id = id_map[type.downcase.to_sym][old_id]
-        raise "invalid placeholder" if new_id.nil?
+        raise OSM::APIBadUserInput.new("Placeholder #{type} not found for reference #{old_id} in relation #{self.id.nil? ? placeholder_id : self.id}.") if new_id.nil?
         [type, new_id, role]
       else
         [type, id, role]