Skip to content

Commit 893c3ca

Browse files
committed
Fix bug with nullified foreign key back to root record
1 parent 51168fc commit 893c3ca

File tree

6 files changed

+17
-6
lines changed

6 files changed

+17
-6
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Fixed
11+
12+
- Bug with null foreign key to back to auxiliary `has_one` association with not matching names. E.g. user has many profiles and has one default profile, profile belongs to user.
13+
1014
## [0.6.0] - 2024-06-18
1115

1216
### Added

lib/evil_seed/relation_dumper.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ def setup_belongs_to_reflections
172172
model_class.reflect_on_all_associations(:belongs_to).reject do |reflection|
173173
next false if reflection.options[:polymorphic] # TODO: Add support for polymorphic belongs_to
174174
included = root.included?("#{association_path}.#{reflection.name}")
175-
excluded = root.excluded?("#{association_path}.#{reflection.name}") || reflection.name == inverse_reflection
175+
excluded = root.excluded?("#{association_path}.#{reflection.name}")
176+
inverse = reflection.name == inverse_reflection
176177
if excluded and not included
177178
if model_class.column_names.include?(reflection.foreign_key)
178179
puts(" -- excluded #{reflection.foreign_key}") if verbose
@@ -182,7 +183,7 @@ def setup_belongs_to_reflections
182183
foreign_keys[reflection.name] = reflection.foreign_key
183184
table_names[reflection.name] = reflection.table_name
184185
end
185-
excluded and not included
186+
excluded and not included or inverse
186187
end
187188
end
188189

test/db/models.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# frozen_string_literal: true
22

33
class User < ActiveRecord::Base
4-
has_one :profile
4+
has_many :profiles
5+
has_one :default_profile, -> { where(default: true) }, class_name: 'Profile'
56
belongs_to :forum
67

78
has_many :questions, foreign_key: :author_id

test/db/schema.rb

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def create_schema!
2222
create_table :profiles do |t|
2323
t.references :user, foreign_key: { on_delete: :cascade }
2424
t.string :name
25+
t.boolean :default, default: false
2526
t.string :title
2627
end
2728

test/db/seeds.rb

+4-3
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@
5555
Question.create!(question_attrs)
5656

5757
Profile.create!([
58-
{ user: users[0], name: "Profile for user 0", title: "Title for user 0" },
59-
{ user: users[1], name: "Profile for user 1", title: "Title for user 1" },
60-
{ user: users[2], name: "Profile for user 2", title: "Title for user 2" },
58+
{ user: users[0], default: true, name: "Default profile for user 0", title: "Default title for user 0" },
59+
{ user: users[0], default: false, name: "Profile for user 0", title: "Title for user 0" },
60+
{ user: users[1], default: true, name: "Profile for user 1", title: "Title for user 1" },
61+
{ user: users[2], default: true, name: "Profile for user 2", title: "Title for user 2" },
6162
])

test/evil_seed_test.rb

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def setup
1010
root.exclude('forum.users')
1111
root.exclude(/parent\.users/)
1212
root.exclude(/role\..+/)
13+
root.exclude(/\.profiles/)
1314
end
1415
config.root('Question') do |root|
1516
root.exclude(/.*/)
@@ -61,6 +62,8 @@ def test_it_dumps_and_restores
6162
assert Role.find_by(name: 'Superadmin')
6263
assert Question.find_by(name: 'fourth')
6364
assert Question.find_by(name: 'fifth')
65+
assert (Profile.count == 2) # Only default profiles for included users
66+
assert_equal 1, User.find_by(login: 'johndoe').profiles.count
6467
assert Profile.where.not(name: nil).none?
6568
end
6669
end

0 commit comments

Comments
 (0)