Home > Blockchain >  How to create bulk records in rails(V4.2.3)
How to create bulk records in rails(V4.2.3)

Time:04-25

I have tried to crate multiple records at once, but getting error.

example-1: Job.find(5601).job_permissions.insert_all(recruiter_id: recruiter.id, job_access_action_id: job_access_action_id.id) end

example -2 Job.find(5601).job_permissions.bulk_insert(recruiter_id: recruiter.id, job_access_action_id: job_access_action_id.id)

errors: NoMethodError: JobPermission Load (0.3ms) SELECT job_permissions.* FROM job_permissions WHERE job_permissions.job_id = 5601 undefined method `insert_all' for #JobPermission::ActiveRecord_Associations_CollectionProxy:0x0055c4da1ceea0

CodePudding user response:

You need to call insert_all on an ActiveRecord class, not association_proxy as you do now, so:

Job.insert_all([{recruiter_id: ..., {recruiter_id: ...}])

CodePudding user response:

The create method takes also an array as a parameter.

JobPermission.create(
  [
    { job_id: job.id, recruiter_id: recruiter.id, job_access_action_id: job_access_action_id.id },
    ...
  ]
)
  • Related