Manually Set The ID Of A Record When Creating Or Seeding In Rails

Posted By Weston Ganger

I've needed to make a new record have a specific ID in the past. This was an issue becuase ID is attr_protected.

We have two ways we can do this. But I recommend the Method 1 because it only uses one database call and won't auto-increment the id column in the table.

# Method 1
post ={content: "my post content"}) = 10

# Method 2
Post.create({content: "my post content"}){|post| = 10}

Related External Links:

Article Topic:Software Development - Ruby / Rails

Date:November 29, 2015