top | item 30848981

(no title)

brandonbloom | 3 years ago

> the fact that you would often find `merchant`, `account`, `invoice`, etc used as method parameters that represented the _ID_ of the resource rather than the resource itself

I've encountered a few Rails projects in the wild that do this. One solution is to make liberal use of the `to_param` method. This method converts objects to strings that are intended for use in URLs. Of particular note, it's the identity function for strings and numbers, but returns `.id.to_s` for ActiveRecord models. Using this within definitions makes your function polymorphic for whether it accepts a model or an id.

If you do this widely, would probably be best to monkey-patch in your own `to_id` method.

discuss

order

clintonb|3 years ago

Pretty much every model/resource has a `token` field, so we just call `invoice.token`, which is always a string.