Rails: Best way to stub out a model with a string ID?

104 Views Asked by At

Say I want to stub out a Product model which will have a product_id field of type string.

Do I add product_id in addition to the default id field that Rails creates?1

rails g model product product_id:string:uniq

Or make product_id the primary key?

rails g model product product_id:primary_key

With the latter option, is there anything else to set up in addition or should it work right away?2 And would storage and retrieval be as simple as:

Product.create product_id: "54c31h"
Product.find("54c31h")

  1. Note that I didn't put :index:uniq. This is because :uniq on its own creates a unique index (i.e. in the schema you would see add_index :products, :product_id, unique: true).
  2. Looks like there are some extra steps:
0

There are 0 best solutions below