Skip to content

Commit cfe9dc9

Browse files
authored
[ruby/hanami] Don't make extra query for update (#9764)
1 parent 26cfbc0 commit cfe9dc9

File tree

2 files changed

+5
-6
lines changed

2 files changed

+5
-6
lines changed

frameworks/Ruby/hanami/app/actions/updates/index.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Index < HelloWorld::Action
1313

1414
def handle(request, response)
1515
worlds = ALL_IDS.sample(queries(request)).map do |id|
16-
world_repo.update(id)
16+
world_repo.update_random_number(id)
1717
end
1818
response.format = :json
1919
response.body = worlds.to_json

frameworks/Ruby/hanami/app/repos/world_repo.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ def find(id)
77
worlds.by_pk(id).one
88
end
99

10-
def update(id)
11-
world = worlds.by_pk(id)
12-
world_hash = world.one.to_h
10+
def update_random_number(id)
11+
world = find(id)
12+
world_hash = world.to_h
1313
new_value = random_id
1414
new_value = random_id while new_value == world_hash[:randomnumber]
15-
world_hash[:randomnumber] = new_value
16-
world.changeset(:update, **world_hash).commit
15+
worlds.where(id: id).update({ randomnumber: new_value })
1716
world_hash
1817
end
1918

0 commit comments

Comments
 (0)