File tree Expand file tree Collapse file tree 6 files changed +24
-0
lines changed
lib/active_storage/attached Expand file tree Collapse file tree 6 files changed +24
-0
lines changed Original file line number Diff line number Diff line change @@ -179,6 +179,10 @@ def attachment_changes #:nodoc:
179
179
@attachment_changes ||= { }
180
180
end
181
181
182
+ def changed_for_autosave? #:nodoc:
183
+ super || attachment_changes . any?
184
+ end
185
+
182
186
def reload ( *) #:nodoc:
183
187
super . tap { @attachment_changes = nil }
184
188
end
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ class ActiveStorageCreateGroups < ActiveRecord ::Migration [ 6.0 ]
4
+ def change
5
+ create_table :groups do |t |
6
+ end
7
+ end
8
+ end
Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ class ActiveStorageCreateUsers < ActiveRecord::Migration[5.2]
4
4
def change
5
5
create_table :users do |t |
6
6
t . string :name
7
+ t . integer :group_id
7
8
end
8
9
end
9
10
end
Original file line number Diff line number Diff line change 1
1
# frozen_string_literal: true
2
2
3
3
require_relative "create_users_migration"
4
+ require_relative "create_groups_migration"
4
5
5
6
ActiveRecord ::Base . establish_connection ( adapter : "sqlite3" , database : ":memory:" )
6
7
ActiveRecord ::Base . connection . migration_context . migrate
7
8
ActiveStorageCreateUsers . migrate ( :up )
9
+ ActiveStorageCreateGroups . migrate ( :up )
Original file line number Diff line number Diff line change @@ -319,6 +319,14 @@ def upload.open
319
319
assert_equal 2736 , @user . avatar . metadata [ :height ]
320
320
end
321
321
322
+ test "updating an attachment as part of an autosave association" do
323
+ group = Group . create! ( users : [ @user ] )
324
+ @user . avatar = fixture_file_upload ( "racecar.jpg" )
325
+ group . save!
326
+ @user . reload
327
+ assert @user . avatar . attached?
328
+ end
329
+
322
330
test "attaching an existing blob to a new record" do
323
331
User . new ( name : "Jason" ) . tap do |user |
324
332
user . avatar . attach create_blob ( filename : "funky.jpg" )
Original file line number Diff line number Diff line change @@ -126,6 +126,7 @@ class User < ActiveRecord::Base
126
126
127
127
class Group < ActiveRecord ::Base
128
128
has_one_attached :avatar
129
+ has_many :users , autosave : true
129
130
end
130
131
131
132
require_relative "../../tools/test_common"
You can’t perform that action at this time.
0 commit comments