How do I properly use the delegate method?
I'm curious how this can be correctly applied to a booking application. If you have a Reservation
and Customer
model, does it make sense to store information such as the first name, last name, and email address on the Reservation model, as well as the Customer model, or is this where delegate could be used?
I'm not sure if this is an obscure way to use it and should be storing this information on both models.
Yep, that could be a good use case. I use it frequently for things like that. It's not really any different than saying reservation.customer&.name
though with the new Ruby safe operator. It's basically doing that for you in the delegate method, but delegate will let you say reservation.name
instead of having to know the customer lives in between.
Thanks for confirming that Chris, I loved the latest episode on Google Maps.
I think delegate is a great way to limit the responsibility of keeping the data in-sync, if Reservation
, Customer
and User
all had individual address attributes and top level attributes, like name, email, phone number, that could be a PITA to keep up-to-date.
Exactly. And if you need to rename it or make it more complex, delegate can still let you call reservation.name
even though it might delegate through a few objects if you had to add like approvals or some other model in the mix that made things more complex.