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")
- 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 seeadd_index :products, :product_id, unique: true
). - Looks like there are some extra steps: