Why i can not to create an object in the db if i add an relationship belong_to?

huangapple go评论67阅读模式
英文:

Why i can not to create an object in the db if i add an relationship belong_to?

问题

class CompanyTaskProgress < ApplicationRecord
 包括 Discard::Model
 属于 :task, 类名: 'Tasking::Task'
 属于 :progress
end

class Tasking
  Task < ApplicationRecord
 有许多 : company_task_progresses, 依赖: :destroy
 有许多 :progresses, 通过: : company_task_progresses
 end
end

ids.each do |id|
   created = CompanyTaskProgress.create(progress_id:, task_id:)
   p 'RESULT', created
end

'结果', #<CompanyTaskProgress id: nil, progress_id: 47, task_id: 1000, discarded_at: nil, created_at: nil, updated_at: nil>
英文:

I have this model, this model its a intermediate table

class CompanyTaskProgress < ApplicationRecord
 include Discard::Model
 belongs_to :task, class_name: 'Tasking::Task'
 belongs_to :progress
end

it worked well if i dont add these lines:

 belongs_to :company_task
 belongs_to :progress

this is my Task model

module Tasking
 class Task < ApplicationRecord
  has_many : company_task_progresses, dependent: :destroy
  has_many :progresses, through: : company_task_progresses
 end
end

only i am trying to create some object with an iteration:

ids.each do |id|
   created = CompanyTaskProgress.create(progress_id:, task_id:)
   p 'RESULT',created
end

i am getting this object that it has not been created in db

'RESULT', #<CompanyTaskProgress id: nil, progress_id: 47, task_id: 1000, discarded_at: nil, created_at: nil, updated_at: nil>

答案1

得分: 4

belongs_to关联默认要求相关记录存在,并且Ruby on Rails添加验证以确保这一点。这意味着当你在模型中添加

belongs_to :company_task

时,Rails期望在该模型的数据库表上有一个company_task_id列,而且你不能创建新记录而不传递一个指向现有CompanyTask记录的company_task_id

不幸的是,你的示例不够清晰。请注意

CompanyTaskProgress.create(progress_id:, task_id:)

是下面这个的简写形式:

CompanyTaskProgress.create(progress_id: process_id, task_id: task_id)

但在你的示例中,你遍历了ids并将一个id传递给了块。不清楚你从哪里获取了要遍历的ids,也不清楚你是否在遍历之外以某种方式设置了progress_idtask_id。如果在each块之前没有设置progress_idtask_id,那么CompanyTaskProgress.create(progress_id:, task_id:)这个快捷方式将无法工作,因为这些值将不会被正确分配。

我建议在该方法中将create更改为create!,然后你将收到一个异常并带有错误消息,告诉你哪些验证失败了。

或者你可能希望告诉Railsbelongs_to关联是optional的,不需要验证存在,然后更改关联的定义为:

belongs_to :company_task, optional: true
英文:

belongs_to associations require the associated record to exist per default, and Ruby on Rails adds validations to ensure that. That means when you add

belongs_to :company_task

to your model, Rails expects that there will be a company_task_id columns on that model's database table, and you cannot create a new record without passing assigning a company_task_id that points to an existing CompanyTask record.

Unfortunately, your example is unclear. Note that

CompanyTaskProgress.create(progress_id:, task_id:)

is a shortcut for

CompanyTaskProgress.create(progress_id: process_id, task_id: task_id)

But in your example, you iterate over ids and pass an id to the block. It is unclear where you got the ids from that you iterate over. And it is unclear if you set progress_id and task_id somehow outside the iteration. If you didn't set progress_id and task_id before the each block, then the CompanyTaskProgress.create(progress_id:, task_id:) shortcut cannot work because those values will not be assigned correctly.

I suggest changing create to create! in that method, then you will get an exception with an error message telling what validations failed.

Or you might want to tell Rails that the belongs_to association is optional and should not be validated to be present, then change the definition of the association to:

belongs_to :company_task, optional: true

答案2

得分: 1

unsaved_object.errors.full_messages 会告诉你对象存在的验证问题。

如果这不起作用,你可以尝试 unsaved_object.save!,如果保存失败,它会抛出异常并提供有用的信息。

有时候数据库问题可能不会像你期望的那样记录在验证错误中。

英文:

I don't know why it's not getting saved, but I can tell you how to find out:

unsaved_object.errors.full_messages

is going to tell you the validation issues that your object is having.

And if that doesn't do the trick, you could try

unsaved_object.save!

which will blow up if it does not save and should spew useful information. Sometimes there are database issues that don't get recorded to the validation errors as well as you might like.

huangapple
  • 本文由 发表于 2023年6月8日 05:15:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76427164.html
  • ruby
  • ruby-on-rails