ActiveRecord :: RecordInvalid:验证失败:左侧必须存在

时间:2015-07-07 21:51:44

标签: ruby-on-rails activerecord

我不知道为什么会发生这种错误。 我正在使用来自主仓库的rails 5。

ActiveRecord::Base.transaction do
  q = Question.new({
    ... some random attrs
  })

  q.tests.build({
    name: 'teste',
    slug: 'teste',
    year: 1990
  })
  q.save!

end
class Question < ActiveRecord::Base
  has_and_belongs_to_many :tests, join_table: :tests_questions
end
class Test < ActiveRecord::Base
  has_and_belongs_to_many :questions, join_table: :tests_questions
end

跟踪的某些部分:

ActiveRecord::RecordInvalid: Validation failed: Left side must exist
activerecord/lib/active_record/validations.rb:67:in `raise_validation_error'
activerecord/lib/active_record/validations.rb:43:in `save!'
activerecord/lib/active_record/attribute_methods/dirty.rb:29:in `save!'
activerecord/lib/active_record/transactions.rb:303:in `block in save!'
activerecord/lib/active_record/transactions.rb:374:in `block in with_transaction_returning_status'
activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb:210:in `transaction'
activerecord/lib/active_record/transactions.rb:210:in `transaction'
activerecord/lib/active_record/transactions.rb:371:in `with_transaction_returning_status'
activerecord/lib/active_record/transactions.rb:303:in `save!'
activerecord/lib/active_record/associations/has_many_through_association.rb:84:in `save_through_record'
activerecord/lib/active_record/associations/has_many_through_association.rb:47:in `insert_record'
activerecord/lib/active_record/autosave_association.rb:379:in `block in save_collection_association'
activerecord/lib/active_record/autosave_association.rb:370:in `each'
activerecord/lib/active_record/autosave_association.rb:370:in `save_collection_association'
activerecord/lib/active_record/autosave_association.rb:184:in `block in add_autosave_association_callbacks'
activerecord/lib/active_record/autosave_association.rb:157:in `instance_eval'
activerecord/lib/active_record/autosave_association.rb:157:in `block in define_non_cyclic_method'
activesupport/lib/active_support/callbacks.rb:428:in `block in make_lambda'
activesupport/lib/active_support/callbacks.rb:229:in `call'
activesupport/lib/active_support/callbacks.rb:229:in `block in halting_and_conditional'
activesupport/lib/active_support/callbacks.rb:502:in `call'
activesupport/lib/active_support/callbacks.rb:502:in `block in call'
activesupport/lib/active_support/callbacks.rb:502:in `each'
activesupport/lib/active_support/callbacks.rb:502:in `call'
activesupport/lib/active_support/callbacks.rb:90:in `run_callbacks'
activerecord/lib/active_record/callbacks.rb:305:in `_create_record'
activerecord/lib/active_record/timestamp.rb:57:in `_create_record'
activerecord/lib/active_record/persistence.rb:521:in `create_or_update'
activerecord/lib/active_record/callbacks.rb:301:in `block in create_or_update'
activesupport/lib/active_support/callbacks.rb:117:in `call'
activesupport/lib/active_support/callbacks.rb:117:in `call'
activesupport/lib/active_support/callbacks.rb:558:in `block (2 levels) in compile'
activesupport/lib/active_support/callbacks.rb:501:in `call'
activesupport/lib/active_support/callbacks.rb:501:in `call'
activesupport/lib/active_support/callbacks.rb:90:in `run_callbacks'
activerecord/lib/active_record/callbacks.rb:301:in `create_or_update'
activerecord/lib/active_record/suppressor.rb:41:in `create_or_update'
activerecord/lib/active_record/persistence.rb:151:in `save!'
activerecord/lib/active_record/validations.rb:43:in `save!'
activerecord/lib/active_record/attribute_methods/dirty.rb:29:in `save!'
activerecord/lib/active_record/transactions.rb:303:in `block in save!'
activerecord/lib/active_record/transactions.rb:374:in `block in with_transaction_returning_status'
activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb:210:in `transaction'
activerecord/lib/active_record/transactions.rb:210:in `transaction'
activerecord/lib/active_record/transactions.rb:371:in `with_transaction_returning_status'
activerecord/lib/active_record/transactions.rb:303:in `save!'

当我使用=方法而不是build时,有效记录会忽略join_table,将表名选为questions_tests而不是tests_questions

例如:

q.tests = [] of tests
NoMethodError: undefined method `questions_tests' for #<Question:0x0000000516b188>

1 个答案:

答案 0 :(得分:0)

this issue中解决了问题。

如@pixeltrix所述:

  

嗯,看起来像是   构造的连接模型未指向正确的列,因此可以看到   为零。以前这不是问题,但是由于我们需要   默认情况下,belongs_to关联现在会爆炸。简单的解决方法是   只是在:left_side关联上放可选:true   构建,但我认为我们可以做得更好