Skip to content

Commit f1eb014

Browse files
committed
Clear the model's association cache before resolving its association
1 parent 24b30be commit f1eb014

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

lib/ruby_lsp/ruby_lsp_rails/server.rb

+2
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ def resolve_association_target(params)
152152
}
153153
end
154154

155+
# Clear the cache so that we get the latest associations.
156+
const.clear_reflections_cache
155157
association_klass = const.reflect_on_association(params[:association_name].intern).klass
156158

157159
source_location = Object.const_source_location(association_klass.to_s)

sorbet/rbi/shims/user.rbi

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# typed: strict
2+
# frozen_string_literal: true
3+
4+
class User
5+
class << self
6+
sig { params(association_name: Symbol).void }
7+
def has_many(association_name)
8+
end
9+
10+
sig { returns(T::Hash[String, T.untyped]) }
11+
def reflections
12+
end
13+
end
14+
end

test/ruby_lsp_rails/server_test.rb

+20
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,26 @@ def <(other)
103103
assert_nil(response.fetch(:result))
104104
end
105105

106+
test "resolve association reflect the latest associations" do
107+
response = @server.execute(
108+
"association_target_location",
109+
{ model_name: "User", association_name: :memberships },
110+
)
111+
assert_nil(response.fetch(:result))
112+
113+
User.has_many(:memberships)
114+
115+
response = @server.execute(
116+
"association_target_location",
117+
{ model_name: "User", association_name: :memberships },
118+
)
119+
120+
location = response[:result][:location]
121+
assert_match(%r{test/dummy/app/models/membership.rb:3$}, location)
122+
ensure
123+
User.reflections.delete("memberships")
124+
end
125+
106126
test "resolve association handles class_name option" do
107127
response = @server.execute(
108128
"association_target_location",

0 commit comments

Comments
 (0)