July 26, 2022
This blog is part of our Rails 7 series.
Previously, saving an attachment to a record returned a boolean, indicating
whether or not the attachment was saved to the record. This is not helpful since
we have to dig into the record again to retrieve the attachment. Starting Rails
7.1, saving an attachment to a record returns the saved attachment. We can now
conveniently use blob methods like #download
, #url
, #variant
etc on the
attachment without having to dig into the record again.
# rails console
>> @user = User.create!(name: "Josh")
>> @user.avatar.attach(params[:avatar])
=> true
# rails console
>> @user = User.create!(name: "Josh")
>> @user.avatar.attach(params[:avatar])
=> #<ActiveStorage::Attached::One:0x00007f075e592380 @name="avatar" @record=#<User:0x00007f075e5924e8 id: "1", name: "Josh">
Please check out this pull request for more details.
If this blog was helpful, check out our full blog archive.