【问题标题】:Rails 6. Transferring an attached file from the instance of one model to an instance of another modelRails 6. 将附件从一个模型的实例传输到另一个模型的实例
【发布时间】:2020-01-11 02:31:58
【问题描述】:

我有一个 User 模型和一个 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

Spkr

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 行)。

好的,截至今天上午的更多信息......(2020 年 11 月 1 日)

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上传并附加到用户模型的相同图像。
  • 是的,这很好!不过,我仍然有兴趣知道如何解决它。 @SantoshAryal
  • 好吧,你能分享一下完整动作的日志吗?
  • 你的意思是在make_spkr函数开始运行之前?
  • 是的,请粘贴日志。

标签: ruby-on-rails ruby rails-activestorage ruby-on-rails-6


【解决方案1】:

在将图像附加到 Spkr 时,您正在标记 current_user 的活动存储附件 id 而不是 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

它现在应该可以工作了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-23
    • 2015-02-27
    • 2011-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多