Quantcast
Channel: Active questions tagged ruby - Stack Overflow
Viewing all articles
Browse latest Browse all 4631

Ruby on Rails ActiveRecord and the primary key

$
0
0

When using Ruby on Rails ActiveRecord to upload and store the name of a file in the partition table database, and the primary key is obtained:

After uploading the A file and storing its name in the database, the ID is set to auto-increment. When saving B files to the database, you intend to use the primary key obtained after saving A file. The code looks like this:

a_file_model = AFiles.create!(  a_file_name: a_file_name,)a_file_id = a_file_model.idlogger.info("a_file_id: #{a_file_id.inspect}")b_file_array.each do |b_file|  b_file_model = BFiles.create!(    b_file_name: b_file,    a_file_id: a_file_id  )end

However, the value of a_file_id appears as nil. What should i do?

I do not want to establish a relationship between table A and table B; I just want to use the ID.

I want to use the ID obtained after saving file A when storing file B.

I would greatly appreciate it if you could let me know.


Viewing all articles
Browse latest Browse all 4631

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>