ruby-on-rails Ruby on Rails如何为附件记录添加.count

slsn1g29  于 2023-03-20  发布在  Ruby
关注(0)|答案(1)|浏览(126)

我收到此错误

PG::UndefinedColumn: ERROR:  column cases.invoice_id does not exist
LINE 1: SELECT COUNT(*) FROM "cases" WHERE ("cases"."invoice_id" IS ...
                                            ^
: SELECT COUNT(*) FROM "cases" WHERE ("cases"."invoice_id" IS NOT NULL)

我试图计数所有的发票,我做这个代码

All the Invoice (<%= Case.where.not(invoice: nil).count %>)</h3>

我怎样才能让它工作呢?

class AddAttachmentInvoiceToCases < ActiveRecord::Migration
  def self.up
    change_table :cases do |t|
      t.attachment :invoice
    end
  end

  def self.down
    remove_attachment :cases, :invoice
  end
end

<%= link_to "Your invoice link", item.invoice.url, target: "_blank" %> <% if item.created_at.present? %>
ocebsuys

ocebsuys1#

我相信你使用的是回形针,当你在迁移文件中指定t.attachment :invoice时,Rails会将这些列添加到cases表中:
1.发票文件名
1.发票文件大小
1.发票内容类型
1.发票更新日期
因此,使用其中一列(最好是invoice_file_name)检查附件是否存在。

Case.where.not(invoice_file_name: nil).count

相关问题