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

时间:2019-02-20 04:43:14

标签: ruby-on-rails activerecord

我有一个称为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)

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

这是完整的堆栈跟踪:

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。

0 个答案:

没有答案