“多态关联不支持计算类。”当尝试保存没有关联的记录时

问题描述 投票:0回答:1

我有一个名为 Bill 的 ActiveRecord 模型,它具有多态关联:bill_elements

has_many :sites, through: :bill_elements, source: :element, source_type: 'Site'
has_many :primary_billing_points, through: :bill_elements, source: :element, source_type: 'PrimaryBillingPoint'

我想确保至少选择一个账单元素并因此添加

validates :bill_elements, length: { minimum: 1 }

但是,一旦用户单击“保存”,我就会收到错误:

Polymorphic association does not support to compute class.

控制器执行

@bill.update(bill_attributes)

验证似乎没有运行。我尝试在其中添加带有 put 语句的自定义验证,如果我更新任何其他字段,我会看到 put 语句,但如果删除关联的站点/主要计费点则不会。

这是完整的堆栈跟踪:

ActiveRecord::Reflection::BelongsToReflection#compute_class
activerecord (5.2.1) lib/active_record/reflection.rb, line 420
ActiveRecord::Reflection::BelongsToReflection#klass
activerecord (5.2.1) lib/active_record/reflection.rb, line 379
ActiveRecord::Associations::HasManyThroughAssociation#delete_records
composite_primary_keys (11.0.3) lib/composite_primary_keys/associations/has_many_through_association.rb, line 22
ActiveRecord::Associations::HasManyThroughAssociation#remove_records
activerecord (5.2.1) lib/active_record/associations/collection_association.rb, line 398
ActiveRecord::Associations::HasManyThroughAssociation#remove_records
activerecord (5.2.1) lib/active_record/associations/has_many_through_association.rb, line 108
block in ActiveRecord::Associations::HasManyThroughAssociation#delete_or_destroy
activerecord (5.2.1) lib/active_record/associations/collection_association.rb, line 391
block in ActiveRecord::Associations::HasManyThroughAssociation#transaction
activerecord (5.2.1) lib/active_record/associations/collection_association.rb, line 134
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter#transaction
activerecord (5.2.1) lib/active_record/connection_adapters/abstract/database_statements.rb, line 252
Site.transaction
activerecord (5.2.1) lib/active_record/transactions.rb, line 212
ActiveRecord::Associations::HasManyThroughAssociation#transaction
activerecord (5.2.1) lib/active_record/associations/collection_association.rb, line 133
ActiveRecord::Associations::HasManyThroughAssociation#delete_or_destroy
activerecord (5.2.1) lib/active_record/associations/collection_association.rb, line 391
ActiveRecord::Associations::HasManyThroughAssociation#delete
activerecord (5.2.1) lib/active_record/associations/collection_association.rb, line 188
ActiveRecord::Associations::HasManyThroughAssociation#replace_records
activerecord (5.2.1) lib/active_record/associations/collection_association.rb, line 412
block in ActiveRecord::Associations::HasManyThroughAssociation#replace
activerecord (5.2.1) lib/active_record/associations/collection_association.rb, line 250
block in ActiveRecord::Associations::HasManyThroughAssociation#transaction
activerecord (5.2.1) lib/active_record/associations/collection_association.rb, line 134
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter#transaction
activerecord (5.2.1) lib/active_record/connection_adapters/abstract/database_statements.rb, line 252
Site.transaction
activerecord (5.2.1) lib/active_record/transactions.rb, line 212
ActiveRecord::Associations::HasManyThroughAssociation#transaction
activerecord (5.2.1) lib/active_record/associations/collection_association.rb, line 133
ActiveRecord::Associations::HasManyThroughAssociation#replace
activerecord (5.2.1) lib/active_record/associations/collection_association.rb, line 250
ActiveRecord::Associations::HasManyThroughAssociation#ids_writer
composite_primary_keys (11.0.3) lib/composite_primary_keys/associations/collection_association.rb, line 26
Bill#site_ids=
activerecord (5.2.1) lib/active_record/associations/builder/collection_association.rb, line 65
Bill#_assign_attribute
activemodel (5.2.1) lib/active_model/attribute_assignment.rb, line 51
block in Bill#_assign_attributes
activemodel (5.2.1) lib/active_model/attribute_assignment.rb, line 44
Bill#_assign_attributes
activemodel (5.2.1) lib/active_model/attribute_assignment.rb, line 43
Bill#_assign_attributes
activerecord (5.2.1) lib/active_record/attribute_assignment.rb, line 23
Bill#assign_attributes
activemodel (5.2.1) lib/active_model/attribute_assignment.rb, line 35
block in Bill#update
activerecord (5.2.1) lib/active_record/persistence.rb, line 427
block in Bill#with_transaction_returning_status
activerecord (5.2.1) lib/active_record/transactions.rb, line 387
block in ActiveRecord::ConnectionAdapters::PostgreSQLAdapter#transaction
activerecord (5.2.1) lib/active_record/connection_adapters/abstract/database_statements.rb, line 254
block in ActiveRecord::ConnectionAdapters::TransactionManager#within_new_transaction
activerecord (5.2.1) lib/active_record/connection_adapters/abstract/transaction.rb, line 239
ActiveSupport::Concurrency::LoadInterlockAwareMonitor#mon_synchronize
/Users/jeremynagel/.rvm/rubies/ruby-2.3.7/lib/ruby/2.3.0/monitor.rb, line 214
ActiveRecord::ConnectionAdapters::TransactionManager#within_new_transaction
activerecord (5.2.1) lib/active_record/connection_adapters/abstract/transaction.rb, line 236
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter#transaction
activerecord (5.2.1) lib/active_record/connection_adapters/abstract/database_statements.rb, line 254
Bill.transaction
activerecord (5.2.1) lib/active_record/transactions.rb, line 212
Bill#with_transaction_returning_status
activerecord (5.2.1) lib/active_record/transactions.rb, line 385
Bill#update
activerecord (5.2.1) lib/active_record/persistence.rb, line 426
block in Admin::BillsController#update
app/controllers/admin/bills_controller.rb, line 84
Admin::BillsController#respond_to
actionpack (5.2.1) lib/action_controller/metal/mime_responds.rb, line 197
Admin::BillsController#update
app/controllers/admin/bills_controller.rb, line 83

我正在使用 Rails 5.2。

ruby-on-rails activerecord
1个回答
0
投票

多态关联不支持计算类。

我正在使用 Rails 7.0.8.4、gem 'ransack' 和 ruby“3.1.6”

以下代码会生成错误

@q = Audited::Audit.ransack(params[:q])

enter image description here

注意:此代码在 Rails 4 中运行良好,但在 Rails 7 中不起作用

© www.soinside.com 2019 - 2024. All rights reserved.