Category Archives: Knowledge Dump

Many to Many joins with Ecto and Elixir

Ecto is an DSL for writing queries and interacting with databases in Elixir.  It has a style similar to ActiveRecord, but since there aren’t objects, you can’t really call it an ORM.

Like ActiveRecord, it has has_many and belongs_to, but it doesn’t have has_many :through yet. Here is how I implemented a DAG using a many-to-many join in Ecto anyways

The basic idea is I have a Nodes table.  And each Node can have multiple parents and multiple children.  The most straightforward implementation of this is to have a many-to-many join table and then use that with INNER JOIN to query for parents or children.

First, I needed to create the tables using Ecto’s migration feature.

Then I define the models using the Ecto model DSL.

This wasn’t quite enough though. I ran into trouble getting Node to preload its children and parents via NodeToNode. I ended up adding help functions to the Node module.

This now lets me write an association.

This works as far as I can tell with my unittests. I’m not completely comfortable with Ecto’s association proxy, so there might be a better way to do this that is more idiomatic. I hope that has_many :through support is added soon so I can concentrate on my business logic instead of mundane joins. I think more documentation of reading and writing associations is necessary.

If you know a better way to express this, please let me know.

Require.js and HAML

I’ve been having very good luck with requirehaml. I had been looking around at various templates, but none of them seemed to support compiling before delivery to the client. This script does that and compiles them into easily verifiable javascript files, packaged up to be included with my existing require.js AMD build.

So far, I haven’t actually had that much HTML to include, but hopefully that will change as my project takes shape and grows.

Now I just need to find something to do the same for SASS.

How to setup a pretty good dev environment on the mac

(This was a quick write up for a coworker, but others might find it useful)

This assumes you have XCode. I mean, really now.

First thing to do is install mac homebrew. It’s a package manager of useful open source software. You install it by pasting into the terminal:

/usr/bin/ruby -e "$(curl -fsSL https://raw.github.com/gist/323731)"

then just to make sure it’s working, run:

brew install -v git

Next we want to install the node package manager.

It’s a simple

curl http://npmjs.org/install.sh | sh

Lastly, I like to setup rvm, for managing multiple ruby installations and keeping my base machine clean of stray gems.

bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)

RVM is a little more complicated since you also have to add a little something to your shell file so that the paths are loaded correctly

echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"

That's *about* it. I'm still looking into how to setup the RVM equivalent for python, but it seems that community is still figuring out their tools and a clear solution hasn't emerged yet.

AppleEvents: Apple’s worst documented API?

If you are trying to register a callback on getting a URL passed to your program through AppleEvents, don’t use the documented kAEInternetSuite and kAEISGetURL enums. They are defined as ‘gurl’, but the actual value passed to your program is ‘GURL’. From what I can tell, everyone defines their own enums because this is well known.

Apple can’t fix it though, because it would break everyone’s software. So they keep documenting it as ‘gurl’, and we keep redefining it to the actual value.

Awesome.