Skip to content

Commit dc28a4f

Browse files
authored
fix: correct issue with many_to_many when one of the models has a prefix to the intersection model association (#449)
1 parent 3cc2983 commit dc28a4f

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

lib/graphiti/adapters/active_record/many_to_many_sideload.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
class Graphiti::Adapters::ActiveRecord::ManyToManySideload < Graphiti::Sideload::ManyToMany
22
def through_table_name
3-
@through_table_name ||= parent_resource_class.model
4-
.reflections[through.to_s].klass.table_name
3+
@through_table_name ||= resource_class.model.reflections[through.to_s].klass.table_name
54
end
65

76
def through_relationship_name

spec/fixtures/legacy.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,16 @@
9595
t.integer :mentor_id
9696
t.integer :mentee_id
9797
end
98+
99+
create_table :sales_shops do |t|
100+
t.string :name
101+
end
102+
103+
create_table :sales_stocks do |t|
104+
t.integer :book_id
105+
t.integer :shop_id
106+
t.integer :amount
107+
end
98108
end
99109

100110
module Legacy
@@ -186,6 +196,8 @@ class Book < ApplicationRecord
186196
has_many :tags, through: :taggings
187197
has_many :readerships
188198
has_many :readers, through: :readerships, source: :user
199+
has_many :sales_stocks, class_name: "Legacy::Sales::Stock"
200+
has_many :sales_shops, class_name: "Legacy::Sales::Shop", through: :sales_stocks, source: :shop
189201
end
190202

191203
class User < ApplicationRecord
@@ -208,6 +220,22 @@ class Tagging < ApplicationRecord
208220
belongs_to :tag
209221
end
210222

223+
module Sales
224+
def self.table_name_prefix
225+
"sales_"
226+
end
227+
228+
class Stock < ApplicationRecord
229+
belongs_to :book
230+
belongs_to :shop
231+
end
232+
233+
class Shop < ApplicationRecord
234+
has_many :stocks
235+
has_many :books, through: :stocks, inverse_of: :sales_shops
236+
end
237+
end
238+
211239
class LegacyApplicationSerializer < Graphiti::Serializer
212240
end
213241

@@ -245,6 +273,20 @@ class BookResource < ApplicationResource
245273
many_to_many :readers, resource: Legacy::UserResource
246274
end
247275

276+
module Sales
277+
class StockResource < ApplicationResource
278+
attribute :amount, :integer
279+
belongs_to :book
280+
belongs_to :shop
281+
end
282+
283+
class ShopResource < ApplicationResource
284+
attribute :name, :string
285+
has_many :stocks
286+
many_to_many :books, resource: Legacy::BookResource, foreign_key: {sales_stocks: :shop_id}
287+
end
288+
end
289+
248290
class StateResource < ApplicationResource
249291
attribute :name, :string
250292
attribute :abbreviation, :string do

spec/integration/rails/finders_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,21 @@ def resource
12861286
expect(author2_hobbies.size).to eq(1)
12871287
end
12881288

1289+
context "when sideloads non-namespace model via a namespace model" do
1290+
let(:shop) { Legacy::Sales::Shop.create(name: "shop") }
1291+
1292+
before do
1293+
allow(Legacy::Sales::ShopResource).to receive(:validate_endpoints?) { false }
1294+
allow(controller).to receive(:resource).and_return(Legacy::Sales::ShopResource)
1295+
Legacy::Sales::Stock.create(shop_id: shop.id, book_id: book1.id, amount: 2)
1296+
end
1297+
1298+
it "works" do
1299+
do_index({include: "books"})
1300+
expect(included("books").map(&:id)).to eq([book1.id])
1301+
end
1302+
end
1303+
12891304
context "when configured as a has_many association" do
12901305
before do
12911306
@orig_sideload = Legacy::AuthorResource.sideloads.delete(:hobbies)

0 commit comments

Comments
 (0)