Skip to content

Commit 2205c01

Browse files
committed
Fix backend_apis associations
1 parent dca6ac8 commit 2205c01

1 file changed

Lines changed: 17 additions & 1 deletion

File tree

app/lib/backend_api_logic/service_extension.rb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ def backend_api_config
3333
end
3434

3535
def backend_api
36-
@backend_api ||= backend_api_configs.first&.backend_api || account.backend_apis.build(system_name: service_system_name, name: "#{service_name} Backend", description: "Backend of #{service_name}")
36+
# Return early if we already have a persisted backend_api
37+
return @backend_api if @backend_api&.persisted?
38+
39+
# Try to get backend_api from backend_api_configs, or keep the memoized unpersisted one, or build a new one
40+
@backend_api = backend_api_configs.first&.backend_api || @backend_api || build_backend_api
3741
end
3842

3943
def update!(attrs = {})
@@ -52,6 +56,18 @@ def update(attrs = {})
5256
rescue ActiveRecord::RecordInvalid
5357
false
5458
end
59+
60+
private
61+
62+
def build_backend_api
63+
# Build without adding to association to avoid polluting it with unpersisted records
64+
BackendApi.new(
65+
account: account,
66+
system_name: service_system_name,
67+
name: "#{service_name} Backend",
68+
description: "Backend of #{service_name}"
69+
)
70+
end
5571
end
5672

5773
def backend_api_proxy

0 commit comments

Comments
 (0)