I am currently looking for a software/web development job preferably as a Rails-based developer.
Please email me at weston@westonganger.com. Thanks in advance for any leads!

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 = Post.new({content: "my post content"})
post.id = 10

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

Related External Links:

Article Topic:Software Development - Rails

Date:November 29, 2015

Recommended Posts