Skip to content

Commit ad9aa36

Browse files
committed
Fix for belongs_to_many association with custom keys
1 parent 7d971b0 commit ad9aa36

File tree

6 files changed

+56
-15
lines changed

6 files changed

+56
-15
lines changed

lib/torque/postgresql/associations/belongs_to_many_association.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ class BelongsToManyAssociation < ::ActiveRecord::Associations::CollectionAssocia
1212
## CUSTOM
1313
def ids_reader
1414
if loaded?
15-
target.pluck(reflection.association_primary_key)
15+
target.pluck(reflection.active_record_primary_key)
1616
elsif !target.empty?
17-
load_target.pluck(reflection.association_primary_key)
17+
load_target.pluck(reflection.active_record_primary_key)
1818
else
1919
stale_state || column_default_value
2020
end

lib/torque/postgresql/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
module Torque
44
module PostgreSQL
5-
VERSION = '3.3.1'
5+
VERSION = '3.3.2'
66
end
77
end

spec/schema.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
version = 2
13+
version = 3
1414

1515
return if ActiveRecord::Migrator.current_version == version
1616
ActiveRecord::Schema.define(version: version) do
@@ -77,22 +77,22 @@
7777
create_table "texts", force: :cascade do |t|
7878
t.integer "user_id"
7979
t.string "content"
80-
t.enum "conflict", enum_type: :conflicts
80+
t.enum "conflict", enum_type: :conflicts
8181
end
8282

8383
create_table "comments", force: :cascade do |t|
84-
t.integer "user_id", null: false
84+
t.integer "user_id", null: false
8585
t.integer "comment_id"
8686
t.integer "video_id"
87-
t.text "content", null: false
87+
t.text "content", null: false
8888
t.string "kind"
8989
t.index ["user_id"], name: "index_comments_on_user_id", using: :btree
9090
t.index ["comment_id"], name: "index_comments_on_comment_id", using: :btree
9191
end
9292

9393
create_table "courses", force: :cascade do |t|
9494
t.integer "category_id"
95-
t.string "title", null: false
95+
t.string "title", null: false
9696
t.interval "duration"
9797
t.enum "types", enum_type: :types, array: true
9898
t.datetime "created_at", null: false
@@ -108,7 +108,7 @@
108108
t.integer "activity_id"
109109
t.string "title"
110110
t.text "content"
111-
t.enum "status", enum_type: :content_status
111+
t.enum "status", enum_type: :content_status
112112
t.index ["author_id"], name: "index_posts_on_author_id", using: :btree
113113
end
114114

@@ -120,8 +120,8 @@
120120
end
121121

122122
create_table "users", force: :cascade do |t|
123-
t.string "name", null: false
124-
t.enum "role", enum_type: :roles, default: :visitor
123+
t.string "name", null: false
124+
t.enum "role", enum_type: :roles, default: :visitor
125125
t.datetime "created_at", null: false
126126
t.datetime "updated_at", null: false
127127
end
@@ -137,7 +137,7 @@
137137
t.integer "author_id"
138138
t.string "title"
139139
t.boolean "active"
140-
t.enum "kind", enum_type: :types
140+
t.enum "kind", enum_type: :types
141141
t.datetime "created_at", null: false
142142
t.datetime "updated_at", null: false
143143
end

spec/tests/belongs_to_many_spec.rb

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@
400400
let(:player) { Class.new(ActiveRecord::Base) }
401401
let(:other) { player.create }
402402

403-
# TODO: Set as a shred example
403+
# TODO: Set as a shared example
404404
before do
405405
connection.create_table(:players, id: :uuid) { |t| t.string :name }
406406
connection.create_table(:games, id: :uuid) { |t| t.uuid :player_ids, array: true }
@@ -440,4 +440,45 @@
440440
expect { query.load }.not_to raise_error
441441
end
442442
end
443+
444+
context 'using custom keys' do
445+
let(:connection) { ActiveRecord::Base.connection }
446+
let(:post) { Post }
447+
let(:tag) { Tag }
448+
let(:tags) { %w[a b c].map { |id| create(:tag, friendly_id: id) } }
449+
450+
subject { create(:post) }
451+
452+
before do
453+
connection.add_column(:tags, :friendly_id, :string)
454+
connection.add_column(:posts, :friendly_tag_ids, :string, array: true)
455+
post.belongs_to_many(:tags, foreign_key: :friendly_tag_ids, primary_key: :friendly_id)
456+
post.reset_column_information
457+
tag.reset_column_information
458+
end
459+
460+
after do
461+
tag.reset_column_information
462+
post.reset_column_information
463+
post._reflections.delete(:tags)
464+
end
465+
466+
it 'loads associated records' do
467+
subject.update(friendly_tag_ids: tags.pluck(:friendly_id))
468+
469+
expect(subject.tags.to_sql).to be_eql(<<-SQL.squish)
470+
SELECT "tags".* FROM "tags" WHERE "tags"."friendly_id" IN ('a', 'b', 'c')
471+
SQL
472+
473+
expect(subject.tags.load).to be_a(ActiveRecord::Associations::CollectionProxy)
474+
expect(subject.tags.to_a).to be_eql(tags)
475+
end
476+
477+
it 'can properly assign tags' do
478+
expect(subject.friendly_tag_ids).to be_blank
479+
480+
subject.tags = tags
481+
expect(subject.friendly_tag_ids).to be_eql(%w[a b c])
482+
end
483+
end
443484
end

spec/tests/distinct_on_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
end
4141

4242
it 'raises with invalid relation' do
43-
expect { subject.distinct_on(tags: :name).to_sql }.to \
43+
expect { subject.distinct_on(supervisors: :name).to_sql }.to \
4444
raise_error(ArgumentError, /Relation for/)
4545
end
4646

spec/tests/relation_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def attribute(relation, name)
4444
end
4545

4646
it 'raises on relation not present' do
47-
check = [tags: :name]
47+
check = [supervisors: :name]
4848
expect{ subject.call(check) }.to raise_error(ArgumentError, /Relation for/)
4949
end
5050

0 commit comments

Comments
 (0)
close