Available for Hire!


programming tips & tricks

Jan 8

How to find a model by any attribute in Ember.js

One of the common things people ask about Ember Data is how to find a single record by it’s attribute. This is because the current revision (11) only offers three methods of fetching records

App.User.find(1) // returns a single user record
App.User.find({ username: "wycats" }) // returns a ManyArray
App.User.findQuery({ username: "wycats" }) // same as the above

If you want to search for a user by his username, you have two options

Using .find with smart server side

The way App.User.find(1) works is that it does a request to /users/1, which is expected to return just one record.

You could modify your server to accept both username and id on the /users/1 path, which would allow to do App.User.find("wycats").

There’s an issue with this though. If you load the same user via his username and id, you’ll end up with two records stored in the Ember identity map.

Which basically means that if you try to retrieve all of the user records, you will end up with that one user twice.

If you want to read more about this, checkout this GitHub issue

Using a findQuery

This might not seem like the right solution at first, since it returns a DS.ManyArray instead of just one record, but hang on.

DS.ManyArray is a subclass of DS.RecordArray, which includes a DS.LoadPromise.

To understand how DS.LoadPromise works, we need to understand what promises are. There’s a great article about that, so I won’t go into much detail.

Promise is basically an async monad (I guess that doesn’t help, let’s try again).

Promise is something which allows you to return an object which wraps around a value, even if you don’t have the value yet. For example if you’re doing App.User.findQuery, you’ll get back an empty DS.ManyArray instantly.

It doesn’t wait until the AJAX request is finished, it just returns the empty array, which is populated with the data once the request finishes.

This works because Ember uses data bindings and will automagically update all of the views once the data is loaded. And also because the router will wait if it’s model has a state isLoading. That way you won’t display a page which is half loaded.


Now that we know we’re getting a DS.ManyArray, we need to figure out a way to make it represent only the value of it’s first element, because that’s what we care about.

var users = App.User.findQuery({ username: username });

users.one("didLoad", function() {

return users;

You can see that we are returning the result of the findQuery instantly, but we’re also setting an asynchronous callback which resolves the promise to the firstObject once it is loaded.

Another way you could read the resolve(x) is from now you’re representing value x. Using this technique will work in all Ember, because the data bindings will take care of everything. Always remember that you don’t need to worry about re-rendering your views, just change the data and Ember will take care of the rest.

Sep 22

Make your Rails development faster with Ruby-perf and Zeus

Nothing is perfect and even a great framework like Ruby on Rails has its weak parts, and one of those is speed.

But I am not talking about speed in production where you can scale up to 50 instances in your cloud service and nobody really cares. I am talking about development speed. When your application starts growing and you start adding gems, it starts to take really long to start up, be it the server, console or just running a single spec.

Now in TDD/BDD, we try to combat this by either writing Rails-less specs which just use plain classes, or use something like spork. I’ve been happily using spork myself for quite some time, but there are things which spork just doesn’t do.

One of those is launching a console or starting a server. But there’s another gem that can do this for all of your Rails commands, and that is zeus. Zeus is smart, you don’t have to put it in your Gemfile or run it with Bundler, all you need to do is create a JSON config file via zeus init and then start the server zeus start.

After that, you’re ready to go, all you need to do is prefix every command with zeus. That means rails server becomes zeus server, rails console becomes zeus console, and so on.

There is one big advantage to this over spork, and that is you don’t need to add it to your project’s Gemfile and spec_helper. You just run it.

But this is not the end of the story. Zeus was designed to work with the new Ruby garbage collector. There is a patched version of Ruby which has backported the new GC from Ruby 2.0 dev, with some additional performance tweaks.

You can either install it via rvm

rvm reinstall 1.9.3-perf --patch falcon --force-autoconf -j 3

or rbenv

curl https://raw.github.com/gist/1688857/rbenv.sh | sh ; rbenv global 1.9.3-p194-perf

more detailed information can be found in this Gist.

From what I’ve tried, ruby-perf gives about 2 times better performance for running specs than 1.9.3, and almost 3 times better than 1.9.2. This of course might vary, depending on the project, but it will give you noticable boost in speed.

The gist also contains some more information about tweaking the GC HEAP, specifically

export RUBY_HEAP_MIN_SLOTS=1000000
export RUBY_GC_MALLOC_LIMIT=100000000
export RUBY_HEAP_FREE_MIN=500000

but you might not want to use this if you don’t have a lot of RAM.

For SCV Rush, the difference in running a single spec (thus Rails boot time) was going down to 4 seconds from almost 12 seconds, since I used 1.9.2 before.

I haven’t really had any issues with ruby-perf and zeus, so I can really recommend it for everyone to at least try it.

Sep 12

What is a Monad - first look

Disclaimer: I have no idea what a Monad actually is, I just want to share my current point of view.

  • Monad is a context in which you execute something.
  • Monad is a container.
  • Monad is a method of encapsulation.
  • Monad is a way to hide the God object.
  • Monad is nothing special, you can define it yourself.
  • Monad is something newbies are afraid of.
  • Monad is not the only thing you will be afraid of.
  • Monad is exactly like a burrito
  • Monad will give you nightmares.

I have no idea what I'm doing

Be sensible about your projects - shameless self promotion

I’ve recently founded a Ruby startup Sensible.io with a couple of my colleagues. Our goal is to create and deliver great applications and make our customers happy.

So far we’ve worked on couple of large projects, and a lot of small ones. We all have many years of experience with Ruby on Rails.

But that’s not all, we are also fluent in JavaScript, including Node.JS and Socket.io, which means even if you need a “live” application, we are the right choice.

We will deploy the application and maintain it for you if that’s what you desire. Or we can just write it and give you the source code, and you can deploy it yourself.

Check us out at http://sensible.io

Functional programming (Haskell) - prelude

It’s been a while since my last post, but I hope to get back to regular writing, so here it starts.

I’ve been trying to get into functional programming for a while, almost a year now, but without any major success. I’ve given a try to Scala, Clojure, even a little bit of Erlang, but nothing really caught me for long enough to trully learn it.

But this changed when Chris Granger announced Light Table. It was the first thing that pulled me back into learning Clojure, but since it’s still in development, I just scratched the surface.

This is not where the story ends, recently I decided that I really have to learn some functional programming. I’ve been avoiding the hard things by using languages like Scala, which is just not good enough for learning purposes.

So here it goes, I decided to learn Haskell. Not just read a book about it, I trully want to learn it, understand it, and write at least one usable application using it.

In Haskell there is just one way to program, you can’t mix in a lot of OOP as you can in Scala, so I will be forced to understand everything.

People say that Haskell is hard in the begining, and then it gets super easy once you pass the initial resistance. I hope that will be my case too.

I’ve started with the book Real World Haskell and soon I’m going to try to Write myself a Scheme in 48 hours in Haskell.

Stay tuned for some articles about functional programming.

May 5

Implementing an autoincrement counter in CoffeeScript

In one of the recent articles we implemented an autoincrement counter function using closures in JavaScript. Here’s the source code:

var counter = (function() {
  var count = 0;
  return function() {
    return ++count;

Read More

Mar 21

Git Coding Guidelines

This article is less of an article and more of a list of guidelines for proper git workflow. Let’s dive right in.

  • master branch is ALWAYS ALWAYS ALWAYS deployable, never work directly on master
  • Development code should be on dev branch and then merged into master once fully tested, that way master awlays stays deployable
  • Every time you start a new task, create a branch from dev and then rebase or merge back

Why all of this nonsense? A simple example:

Read More

Mar 9

Two line solution to drop all data from CouchDB

Today was my first day when I actually had to use CouchDB on a real project. The problem I stumbled upon while developing is that there is no way to just drop all that data in a databse from the beautiful web interface that CouchDB has.

After about 15 minutes of fiddling around with the _deleted attribute, I came to a dead simple solution.

namespace :couch do
  desc "Delete and create database"
  task :wipe do
    system('curl -X DELETE http://localhost:5984/dev')
    system('curl -X PUT http://localhost:5984/dev')

There is definitely a better way to do this, but it’s my first day with CouchDB. One thing I definitely like about the REST API is how simple it is to do something like this. All it takes is just little command line tinkering.

Mar 8
“Common sense is the collection of prejudices acquired by age eighteen” Albert Einstein

Mar 7

Private JavaScript variables with the Module Pattern

In the previous article, we’ve talked about closures which are the building stone for anything even remotely complex in JavaScript. This time, we’ll take a look at private variables.

JavaScript itself doesn’t have the concept of a private or public variable, everything is public. But there’s a way around this. Let’s take a look at our previous example

Read More

Page 1 of 5