Firstly, apologies for the late reply. ππ»
I'm happy that the article helped you!
If I understand your requirements correctly - you want to be able to find_by "country code", but create with random "inhabitants". Something lesser known in Rails - there is a method exactly just for that:
Not sure if it makes any difference with your sample above, but using instance.attributes makes it more generic as we don't have to explicitly mention each attribute.
Hey Roland!
Firstly, apologies for the late reply. ππ»
I'm happy that the article helped you!
If I understand your requirements correctly - you want to be able to find_by "country code", but create with random "inhabitants". Something lesser known in Rails - there is a method exactly just for that:
This would find_by "code", and return if exists. Else, it will create with "code", along with "name" and "inhabitants".
I wrote that off the top of my mind. Can you test to make sure it works?
I solved it a bit differently:
Not sure if it makes any difference with your sample above, but using
instance.attributes
makes it more generic as we don't have to explicitly mention each attribute.Thatβs neat! Yeah, this does seem simpler. Off the top of my head, I canβt think of any differences.