]> git.openstreetmap.org Git - rails.git/commitdiff
Avoid using _id in queries
authorAndy Allan <git@gravitystorm.co.uk>
Wed, 30 Aug 2023 15:45:04 +0000 (16:45 +0100)
committerAndy Allan <git@gravitystorm.co.uk>
Wed, 30 Aug 2023 16:08:16 +0000 (17:08 +0100)
This makes the queries shorter and easier to read.

app/controllers/api/changesets_controller.rb
app/controllers/api/notes_controller.rb
app/controllers/changesets_controller.rb
app/controllers/diary_entries_controller.rb
app/controllers/issues_controller.rb
app/controllers/user_roles_controller.rb
app/models/changeset.rb
test/controllers/diary_entries_controller_test.rb

index a3dff4f1cd7e4fc935986a009c71e9b31ac7c411..7bb7a5a4de14bddb49f988f7f9285d96de074b1e 100644 (file)
@@ -325,7 +325,7 @@ module Api
           raise OSM::APINotFoundError if current_user.nil? || current_user != u
         end
 
-        changesets.where(:user_id => u.id)
+        changesets.where(:user => u)
       end
     end
 
index 83024288db43d9963249c301faca3180f457fce8..e6f391ede96e3767e7c5c08f9c210e3a744a9f8c 100644 (file)
@@ -251,7 +251,7 @@ module Api
       end
 
       # Find the comments we want to return
-      @comments = NoteComment.where(:note_id => notes).order("created_at DESC").limit(result_limit).preload(:note)
+      @comments = NoteComment.where(:note => notes).order("created_at DESC").limit(result_limit).preload(:note)
 
       # Render the result
       respond_to do |format|
index 22d3356b79fee7425865fcff5de6098b2cc0b153..fef4d85eb51273596dfe81ccfc0cad88ba81c19f 100644 (file)
@@ -48,16 +48,16 @@ class ChangesetsController < ApplicationController
 
       if @params[:display_name]
         changesets = if user.data_public? || user == current_user
-                       changesets.where(:user_id => user.id)
+                       changesets.where(:user => user)
                      else
                        changesets.where("false")
                      end
       elsif @params[:bbox]
         changesets = conditions_bbox(changesets, BoundingBox.from_bbox_params(params))
       elsif @params[:friends] && current_user
-        changesets = changesets.where(:user_id => current_user.friends.identifiable)
+        changesets = changesets.where(:user => current_user.friends.identifiable)
       elsif @params[:nearby] && current_user
-        changesets = changesets.where(:user_id => current_user.nearby)
+        changesets = changesets.where(:user => current_user.nearby)
       end
 
       changesets = changesets.where("changesets.id <= ?", @params[:max_id]) if @params[:max_id]
index ea6d1d27601db89980fa6366308b90a5ad996f45..dcb625d836932a9746e2aac99bc5d2952577ae92 100644 (file)
@@ -27,7 +27,7 @@ class DiaryEntriesController < ApplicationController
     elsif params[:friends]
       if current_user
         @title = t ".title_friends"
-        entries = DiaryEntry.where(:user_id => current_user.friends)
+        entries = DiaryEntry.where(:user => current_user.friends)
       else
         require_user
         return
@@ -35,7 +35,7 @@ class DiaryEntriesController < ApplicationController
     elsif params[:nearby]
       if current_user
         @title = t ".title_nearby"
-        entries = DiaryEntry.where(:user_id => current_user.nearby)
+        entries = DiaryEntry.where(:user => current_user.nearby)
       else
         require_user
         return
index 88c7a46acd5f0bb3859f9e1dce567815d4328fd9..c24054f77ff3adf4ab44073cea134c9748403b51 100644 (file)
@@ -24,7 +24,7 @@ class IssuesController < ApplicationController
     if params[:search_by_user].present?
       @find_user = User.find_by(:display_name => params[:search_by_user])
       if @find_user
-        @issues = @issues.where(:reported_user_id => @find_user.id)
+        @issues = @issues.where(:reported_user => @find_user)
       else
         @issues = @issues.none
         flash.now[:warning] = t(".user_not_found")
index b54cd0bd7a3eee82ccfecb01ce14882ba1d7c3cc..469b2c40b626845191e7a5265e873c340da7c4f4 100644 (file)
@@ -22,7 +22,7 @@ class UserRolesController < ApplicationController
     if current_user == @user && @role == "administrator"
       flash[:error] = t("user_role.filter.not_revoke_admin_current_user")
     else
-      UserRole.where(:user_id => @user.id, :role => @role).delete_all
+      UserRole.where(:user => @user, :role => @role).delete_all
     end
     redirect_to user_path(@user)
   end
index f23a4e356e883bd51f0fb0d01cb52897a75fd0ab..ce09438245844be0ff4ba241818973cdf26b5bab 100644 (file)
@@ -170,7 +170,7 @@ class Changeset < ApplicationRecord
       save!
 
       tags = self.tags
-      ChangesetTag.where(:changeset_id => id).delete_all
+      ChangesetTag.where(:changeset => id).delete_all
 
       tags.each do |k, v|
         tag = ChangesetTag.new
index 6365d46e5de0aba5210c181131194d15dde78ca7..ea918cb6bbd765b577d6460901cebba1b9985e71 100644 (file)
@@ -164,7 +164,7 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest
     assert_response :success
     assert_template :new
 
-    assert_nil UserPreference.where(:user_id => user.id, :k => "diary.default_language").first
+    assert_nil UserPreference.where(:user => user, :k => "diary.default_language").first
   end
 
   def test_create
@@ -189,7 +189,7 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest
     # checks if user was subscribed
     assert_equal 1, entry.subscribers.length
 
-    assert_equal "en", UserPreference.where(:user_id => user.id, :k => "diary.default_language").first.v
+    assert_equal "en", UserPreference.where(:user => user, :k => "diary.default_language").first.v
   end
 
   def test_create_german
@@ -216,7 +216,7 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest
     # checks if user was subscribed
     assert_equal 1, entry.subscribers.length
 
-    assert_equal "de", UserPreference.where(:user_id => user.id, :k => "diary.default_language").first.v
+    assert_equal "de", UserPreference.where(:user => user, :k => "diary.default_language").first.v
   end
 
   def test_new_spammy