find_or_create_by
method but I didn’t know it had a very useful companion
create_with
.
@something ||= SomeModel.create_with(some: "stuff").find_or_create_by(foo: "bar")
This will either find a record
SomeModel.where(foo: "bar")
or create it with
SomeModel.create(foo: "bar", some: "stuff")
. Very useful.
I found this command line magic gem some time ago and was using it ever since.
I started using it for synchronizing directories between computers on the same network. But it felt kind of clunky and cumbersome to get the slashes right so that it wouldn’t nest those directories and copy everything. Since both source and destination machine had the same basic directory layout, I thought ‘why not make it easier?’ … e.g. like this:
It uses rsync for the heavy lifting but does the tedious source and destination mangling for you. 😀
You can find the code in this Gist.