我有一个用户模型和一个Spkr模型。
用户:
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_many :tlks, dependent: :destroy
has_many :spkrs, dependent: :destroy
has_many :msgs, dependent: :destroy
has_one_attached :image
extend FriendlyId
friendly_id :slug_candidates, use: :slugged
def slug_candidates
[
:username,
[:username, DateTime.now.to_date]
]
end
end
斯普克尔
class Spkr < ApplicationRecord
belongs_to :tlk
belongs_to :user
has_many :msgs, dependent: :destroy
has_one_attached :image
end
当我的用户模型附加了图像时,当制作 spkr 时,我希望它与生成 spkr 的用户附加相同的图像。
我有一个 SpkrMaker 模块:
module SpkrMaker
def make_spkr
spkr = Spkr.create!(
user: current_user,
tlk: @tlk,
name: current_user.username,
bio: current_user.bio,
)
if current_user.image.present?
ActiveStorage::Attachment.create(
name: 'image',
record_type: 'Spkr',
record_id: spkr.id,
blob_id: current_user.image.id
)
end
end
end
这是在流期间调用的,当它被称为我的服务器日志状态时:
Started POST "/tlks" for ::1 at 2020-01-11 10:39:41 +0000
Processing by TlksController#create as JS
Parameters: {"authenticity_token"=>"1NJc0ZSwo8hL1JHw5karkWNxoWRzHPNx/xecaAQHdN+EdsG/o+yZwdxZXLZLPVbkgiPiZZX6PpaF38VX5etTAw==", "tlk"=>{"title"=>"goooolan"}, "commit"=>"Create Tlk"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 23], ["LIMIT", 1]]
(0.1ms) begin transaction
↳ app/controllers/tlks_controller.rb:34:in `create'
Tlk Exists? (0.3ms) SELECT 1 AS one FROM "tlks" WHERE "tlks"."id" IS NOT NULL AND "tlks"."slug" = ? LIMIT ? [["slug", "goooolan"], ["LIMIT", 1]]
↳ app/controllers/tlks_controller.rb:34:in `create'
Tlk Create (0.4ms) INSERT INTO "tlks" ("user_id", "title", "created_at", "updated_at", "slug", "invite_code") VALUES (?, ?, ?, ?, ?, ?) [["user_id", 23], ["title", "goooolan"], ["created_at", "2020-01-11 10:39:41.866979"], ["updated_at", "2020-01-11 10:39:41.866979"], ["slug", "goooolan"], ["invite_code", 469667]]
↳ app/controllers/tlks_controller.rb:34:in `create'
(0.9ms) commit transaction
↳ app/controllers/tlks_controller.rb:34:in `create'
(0.1ms) begin transaction
↳ lib/spkr_maker.rb:3:in `make_spkr'
Spkr Create (0.5ms) INSERT INTO "spkrs" ("tlk_id", "user_id", "name", "bio", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["tlk_id", 135], ["user_id", 23], ["name", "test"], ["bio", "info about me"], ["created_at", "2020-01-11 10:39:41.871219"], ["updated_at", "2020-01-11 10:39:41.871219"]]
↳ lib/spkr_maker.rb:3:in `make_spkr'
(0.8ms) commit transaction
↳ lib/spkr_maker.rb:3:in `make_spkr'
ActiveStorage::Attachment Load (0.1ms) SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = ? AND "active_storage_attachments"."record_type" = ? AND "active_storage_attachments"."name" = ? LIMIT ? [["record_id", 23], ["record_type", "User"], ["name", "image"], ["LIMIT", 1]]
↳ lib/spkr_maker.rb:9:in `make_spkr'
(0.0ms) begin transaction
↳ lib/spkr_maker.rb:10:in `make_spkr'
Spkr Load (0.2ms) SELECT "spkrs".* FROM "spkrs" WHERE "spkrs"."id" = ? LIMIT ? [["id", 104], ["LIMIT", 1]]
↳ lib/spkr_maker.rb:10:in `make_spkr'
ActiveStorage::Blob Load (0.0ms) SELECT "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = ? LIMIT ? [["id", 51], ["LIMIT", 1]]
↳ lib/spkr_maker.rb:10:in `make_spkr'
(0.0ms) rollback transaction
↳ lib/spkr_maker.rb:10:in `make_spkr'
Completed 422 Unprocessable Entity in 25ms (ActiveRecord: 3.7ms | Allocations: 14233)
ActiveRecord::RecordInvalid (Validation failed: Blob must exist):
lib/spkr_maker.rb:10:in `make_spkr'
app/controllers/tlks_controller.rb:36:in `create
当我在 rails 控制台中运行 User.last.image 时,我得到以下结果:
irb(main):002:0> User.last.image
User Load (0.2ms) SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT ? [["LIMIT", 1]]
=> #<ActiveStorage::Attached::One:0x00007f88a21cda90 @name="image", @record=#<User id: 23, email: "j@test.com", username: "test", bio: "info about me", name: nil, created_at: "2020-01-11 01:56:22", updated_at: "2020-01-11 01:56:48", slug: "test">>
irb(main):003:0>
我不知道问题是什么,并且不够了解服务器日志以找出问题所在。
在这个过程中会创建一个 Spkr,所以第 10 行以上的所有内容都在 SpkrMaker 模块中工作(ActiveStorage::Attachment.create( = 第 10 行(。
好的 截至今天早上的更多信息...(11/01/2020(
irb(main):010:0> User.last.image.id
User Load (0.2ms) SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT ? [["LIMIT", 1]]
ActiveStorage::Attachment Load (0.2ms) SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = ? AND "active_storage_attachments"."record_type" = ? AND "active_storage_attachments"."name" = ? LIMIT ? [["record_id", 23], ["record_type", "User"], ["name", "image"], ["LIMIT", 1]]
=> 51
但
irb(main):011:0> ActiveStorage::Blob.last.id
ActiveStorage::Blob Load (0.2ms) SELECT "active_storage_blobs".* FROM "active_storage_blobs" ORDER BY "active_storage_blobs"."id" DESC LIMIT ? [["LIMIT", 1]]
=> 49
我不知道为什么会有这种差异。
将映像附加到 Spkr 时,您将活动存储附件 ID 标记为 current_user 而不是 blob_id 而不是 blob ID 我已经更新了代码。
module SpkrMaker
def make_spkr
spkr = Spkr.create!(
user: current_user,
tlk: @tlk,
name: current_user.username,
bio: current_user.bio,
)
if current_user.image.present?
ActiveStorage::Attachment.create(
name: 'image',
record_type: 'Spkr',
record_id: spkr.id,
blob_id: current_user.image.blob_id
)
end
end
end
它现在应该可以工作了。