我在连接 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/