From 329b6faf91745b4b6760d975d8b5574c5ffdb181 Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Thu, 30 Apr 2009 23:45:55 +0000 Subject: [PATCH] Use association names with :joins instead of writing the SQL for the join by hand. --- app/models/node.rb | 4 ++-- app/models/relation.rb | 3 ++- app/models/way.rb | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/app/models/node.rb b/app/models/node.rb index d85b22c9b..a4ba76e91 100644 --- a/app/models/node.rb +++ b/app/models/node.rb @@ -131,11 +131,11 @@ class Node < ActiveRecord::Base # shouldn't be possible to get race conditions. Node.transaction do check_consistency(self, new_node, user) - way = WayNode.find(:first, :joins => "INNER JOIN current_ways ON current_ways.id = current_way_nodes.id", + way = WayNode.find(:first, :joins => :way, :conditions => [ "current_ways.visible = ? AND current_way_nodes.node_id = ?", true, self.id ]) raise OSM::APIPreconditionFailedError.new("Node #{self.id} is still used by way #{way.id}.") unless way.nil? - rel = RelationMember.find(:first, :joins => "INNER JOIN current_relations ON current_relations.id=current_relation_members.id", + rel = RelationMember.find(:first, :joins => :relation, :conditions => [ "visible = ? AND member_type='Node' and member_id=? ", true, self.id]) raise OSM::APIPreconditionFailedError.new("Node #{self.id} is still used by relation #{rel.relation.id}.") unless rel.nil? diff --git a/app/models/relation.rb b/app/models/relation.rb index ae9d2f844..09143294c 100644 --- a/app/models/relation.rb +++ b/app/models/relation.rb @@ -242,7 +242,8 @@ 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 ]) + 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 diff --git a/app/models/way.rb b/app/models/way.rb index d37fb1533..64b399133 100644 --- a/app/models/way.rb +++ b/app/models/way.rb @@ -251,7 +251,7 @@ class Way < ActiveRecord::Base # shouldn't be possible to get race conditions. Way.transaction do check_consistency(self, new_way, user) - if RelationMember.find(:first, :joins => "INNER JOIN current_relations ON current_relations.id=current_relation_members.id", + if RelationMember.find(:first, :joins => :relation, :conditions => [ "visible = ? AND member_type='Way' and member_id=? ", true, self.id]) raise OSM::APIPreconditionFailedError.new("You need to make sure that the way with id: #{self.id} is not a member of a relation.") else -- 2.43.2