mysql - 将多个表与 Rails 中的事件存储连接起来

标签 mysql ruby-on-rails

我在连接 2 个表时遇到问题。我习惯于保存文件的事件存储。但我加入了 2 个表,但它不起作用。我正在加入产品的账单。

这是比尔模型:

class Bill < ApplicationRecord
    belongs_to :product
end

这是产品型号

class Product < ApplicationRecord
  has_one_attached :image
  has_many :bills
  def new

  end
  def list
  end
end

我正在使用命令执行表产品

Product.all

然后是结果

[#<Product:0x00007f3d68ee4998
  id: 22,
  created_at: Tue, 25 May 2021 14:16:11.732340000 UTC +00:00,
  updated_at: Tue, 25 May 2021 14:16:11.775029000 UTC +00:00,
  name: "Bánh aghahhhahgha",
  des: "fgkagjagaahghahaghhah",
  money: 2000000.0,
  image: nil>]

image 为零,因为事件存储保存了它。

我正在使用命令执行 Bill 表

Bill.all

这是结果

[#<Bill:0x00007f3d68875008
  id: 8,
  user_id: 1,
  status: nil,
  message: nil,
  created_at: Tue, 25 May 2021 14:32:24.182690000 UTC +00:00,
  updated_at: Tue, 25 May 2021 14:32:24.182690000 UTC +00:00,
  number_phone: "0328267412",
  quality: 3,
  money: 6000000.0,
  product_id: 22>]

我正在使用命令连接 2 个表

Bill.joins(:product).select('bills.*, products.image a
s image, products.name as name')

结果

[#<Bill:0x0000556da2ce7ba0
  id: 8,
  user_id: 1,
  status: nil,
  message: nil,
  created_at: Tue, 25 May 2021 14:32:24.182690000 UTC +00:00,
  updated_at: Tue, 25 May 2021 14:32:24.182690000 UTC +00:00,
  number_phone: "0328267412",
  quality: 3,
  money: 6000000.0,
  product_id: 22>]

各位有什么解决办法吗,请帮帮我! .

最佳答案

当你执行时

bill = Bill.joins(:product).select('bills.*, products.image a
s image, products.name as name')

它实际上试图从产品表中的图像列加载数据。 Active Record 不是这样工作的。 当我们编写 has_one_attached :image 时,我们并未将其与产品表 image 列绑定(bind),而是声明与 active_storage_attachments 表的关联。


这里是解释。

has_one_attached :image,它是product表和active_storage_attachments表之间的关系。

当我们运行 rails active_storage:install 时,这些迁移就会添加到应用程序中。

所以实际关系是

class Bill
  belongs_to :product
end

and

class Product
  has_one :image
end

Note: (`image` for us to access, the real table name is `active_storage_attachments`)

现在您想要的查询是这样的,通过加入产品返回图像。

=> bill = Bill.joins(:product).joins("INNER JOIN active_storage_attachments on active_storage_attachments.record_id = products.id and active_storage_attachments.record_type = 'Product'").select('bills.*, active_storage_attachments.* as image, products.name as name'))

=> bill.image
#<ActiveStorage::Attached::One:0x00007f911d8a2540
 @name="image",
 @record=
  #<Product:0x00007f911dadf448
   id: 1,
   ...
上面示例中的

bill.image 将返回图像记录,它看起来像这样。但它不会执行额外的查询来加载图像,所以不用担心。

访问前端的图像

<%= image_tag url_for(bill.image) %>

这是ActiveStorage的完整指南https://edgeguides.rubyonrails.org/active_storage_overview.html#what-is-active-storage-questionmark

关于mysql - 将多个表与 Rails 中的事件存储连接起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67681493/

相关文章:

ruby-on-rails - 使用类常量作为 Ruby 哈希键

mysql - 如何替换mysql表中列的文本?

mysql - 如何创建一个 WHERE 子句,其中 SQL 查询中有一些必填字段和一些其他可选字段

mysql - 结合 DROP USER 和 DROP DATABASE 与 SELECT .. WHERE 查询?

ruby-on-rails - 如何确定在Rails中有一天是工作日?

ruby-on-rails - Rails 7 和 Zeitwerk 未在测试中加载

mysql - 插入新行或删除旧行(如果已存在)

java - 初始 SessionFactory 创建失败.org.hibernate.HibernateException : Missing column: dept_id

ruby-on-rails - 使用 ruby​​ 访问浏览器的公共(public) `window` 属性

ruby-on-rails - 基于当前的托管服务,我应该将哪些数据库与 Ruby on Rails 一起使用?