Is it good practice to set a custom id value for an ActiveRecord object
I am writing a simple rails app that caches values from a web service. The web service returns a list of objects that look like this:
<objects>
<object>
<id>12345</id>
<name>obj name</name>
</object>
....
</objects>
Is it okay to use the id coming in as the id for my ActiveRecord object if I am guaranteed it is unique... or is it better practice set it as a different attribute and let ActiveRecord handle the id? I have read through the code and it looks like the create method on ActiveRecord does not generate a new id if it is already set. Am I unders开发者_Python百科tanding this correctly?
Thanks
If your id
field is set to autoincrement you won't be able to specify it, at least not easily. However this is dependent on your database.
I suggest storing external ids independently from your own ids. In the long run it'll give you more flexibility and isolation from the third party's id scheme changing.
I would definitely use my own id, simply because when allowing external access there are no guarantees.
精彩评论