ActiveRecord is an extremely powerful tool, allowing us as developers to be more productive by writing less and doing more. Most of the time this less works to our advantage, but sometimes less is not more.


Imagine you have the following models:

CREATE TABLE books (
  id BIGSERIAL PRIMARY KEY,
  title TEXT,
  author TEXT
);

CREATE TABLE chapters (
  id BIGSERIAL PRIMARY KEY,
  book_id BIGINT REFERENCES books(id) NOT NULL,
  title TEXT,
  rank INTEGER
);
class Book < ActiveRecord::Base
  has_many :chapters
end
class Chapter < ActiveRecord::Base
  belongs_to :book
end

Notice that we have a Book model which has title and author attributes, as well as a Chapter model which references a Book (via book_id) and title attributes.

Since ActiveRecord provides easy interfaces for querying from our database, we can quickly find all Books by an author, title, or containing a Chapter.

> Book.where(author: "H. P. Lovecraft")
=> #<ActiveRecord::Relation [#<Book id: 1, title: "Necronomicon", author: "H. P. Lovecraft">, ...]>

> Book.find_by("title = 'Necronomicon'")
=> #<Book id: 1, title: "Necronomicon", author: "H. P. Lovecraft">

> chapter = Chapter.find_by(title: "The Call of Cthulhu")
=> #<Chapter id: 19, book_id: 1, title: "The Call of Cthulhu", rank: 19>
> chapter.book
=> #<Book id: 1, title: "Necronomicon", author: "H. P. Lovecraft">

If you were paying close attention to the above statements, you may have noticed that two of the arguments were a hash, but one used a string. ActiveRecord allows developers to pass a SQL string to be executed, among other things. For the most part, passing a string into ActiveRecord’s querying utilities will work as expected…for the most part.


Once we get into more complex queries, ActiveRecord may help us avoid problems that we’re not always weary of. Notice how both of our Book and Chapter models have a title attribute, what would happen if we tried to join these two tables together while querying based on title?

> Book.joins(:chapters).find_by("title = 'The Call of Cthulhu'")
PG::AmbiguousColumn: ERROR:  column reference "title" is ambiguous
LINE 1: ...ters" ON "chapters"."book_id" = "books"."id" WHERE (title = '...

Ah, an error! It looks like our database did not know which table’s title attribute we were interested in using. Now lets try the same method, but use a hash instead.

> Book.joins(:chapters).find_by(title: "The Call of Cthulhu")
=> nil

Nil? Well, at least this didn’t raise any exceptions. To better understand what happened, let’s take a look at the generated SQL.

SELECT "books".* FROM "books" INNER JOIN "chapters" ON "chapters"."book_id" = "books"."id" WHERE ("books"."title" = 'The Call of Cthulhu') LIMIT 1

Near the end of the query, we see that ActiveRecord automagically selected our Book model to be queried upon. This is very helpful of Rails, but we didn’t want to query by our Books title, but on our Chapters title. Enter nested hashes.

> Book.joins(:chapters).find_by(chapters: {title: "The Call of Cthulhu"})
=> #<Book id: 1, title: "Necronomicon", author: "H. P. Lovecraft">

Perfect! Now if we take a look at the generated SQL, we’ll see that ActiveRecord correctly queried by our Chapters table.

SELECT "books".* FROM "books" INNER JOIN "chapters" ON "chapters"."book_id" = "books"."id" WHERE ("chapters"."title" = 'The Call of Cthulhu') LIMIT 1

Although this won’t fix all problems with ActiveRecord, knowing is half the battle.

I’ve had Google Glass for a few months now, an I’m no stranger to the glaring eyes. After taking a few days for myself to get used to wearing Glass, I realized it would take me longer to get used to the reactions of others.

My first week of wearing Glass in public, I was concerned people would label me as a Glasshole and denounce me. They did not, most people were ecstatic to see a pair in person. I’ve worn them around town, while shopping, and yes, even the movies. That abruptly ended today.

I’ve been wearing my Glass to Gold’s Gym since I first got it, it makes working out super simple, no more worrying about cables getting stuck in machines, or limiting your reach, as it’s bundled securely on your head. When I began wearing them to Gold’s, I was frequently asked about them: what they were, how they work, what they were capable of. A few times I’ve even let complete strangers try them on and test them. I just don’t want people to be perturbed by my wearing of them. I’d like to say that a few friends were made, as they’re a good conversation starter.

Today, a Gold’s Gym employee informed me I was no longer allowed to wear Glass into their facilities. As she threatened that other members may feel uncomfortable due to the outward facing camera on Glass, I noticed she held her phone in hand, camera pointed at me, exactly what she said was not allowed. I questioned her about that, wondering if these numerous peoples taking “selfies” in the mirror, Facebooking their workouts, constantly photographing themselves an others. There are so many other people that use cameras and recoding devices at that gym, I can assure that I’ve been in numerous photos that I’ve never given consent to. Why was this emerging technology being regarded as dangerous, and when would people understand it wasn’t?

All I could think of was how often I see entire film crews recording at that location. There are constant photos being taken, and videos being filmed, since this is such a well-known gym. Even more recently Arnold Schwarzenegger was at that same Gold’s Gym, doing some undercover recording. Yeah, sure, it’s annoying (read: intimidating) when you’re working out and a professional bodybuilder is being photographed for a magazine cover, but I’ve never seen any employee of Gold’s say something to them.

I would expect that a place in such a technologically centered community as Venice, with Google offices right around the corner (which almost bought the location), an a Microsoft office within “walking distance”, that Gold’s Gym employees and members would be much more open to these sorts of things.

If more people understood what Glass was capable, moreover what it isn’t, then people would be more accepting of using it in public. There are numerous reasons that I wouldn’t and can’t record people in the gym, like:

  • Battery life, especially while listening to music, wouldn’t last for very long
  • The screen would be lit up, which is easily noticeable
  • Recording time is limited to 10 seconds, unless manually overwritten
  • Most importantly, I’m in there doing my own thing, not focusing on/staring at others

I just wish that the common misconceptions about Glass could be cleared up to avert issues like this from arising in the future. Maybe even just for starters, Gold’s Gym could follow their own rules and restrict filming for everybody, or let this new an expanding wearables market progress where it needs to, without this unnecessary battling.


Update: March 2, 2014

I was politely contacted by a manager of Gold’s Gym who informed me that the employee did not have the authority, and they did not approve of the banning. I appreciate the followup from Gold’s, and hope more people will open up to these emerging technologies.

This Thursday I presented my progress on my Guy-Manuel de Homem-Christo helmet, had a blast. For those of you who haven’t heard of Guy-Manuel, perhaps you’ve heard of Daft Punk? If not, then hopefully you’ll still find this post interesting.

I purchased the base of the helmet in late 2011, but never actually got around to working on it. Finally, back in December, I found some motiviation to begin working on it. Here’s the presentation I did at the monthly LA Arduino Enthusiasts meetup, for those of you in the area that haven’t been, I highly encourage checking it out.

This is an active project, and I will be fully open sourcing my work. Expect my code, schematics, and circuit board designs to be on my GitHub here.

Progress Pictures

Left side demo HI demo Left side inside helmet

First Progress Of Lights Strobing

Volume Reactive Matrix

Presentation

So as you all may, or may not know, I have recently been working on my first iPhone game.

That’s right, a game. Some of you may not know that games are much different from regular applications, in the programming aspect. I spent a lot of time researching different techniques for developing iPhone games, and the best one I found was Cocos2d, a free and open source game engine for the iPhone SDK.

Cocos2D Banner

Cocos2d is available for download here (for free).

It comes with TONS of example applications, for reference, and knowledge. This application has a slightly different syntax from the standard iPhone SDK Objective-C, but it is very easy to understand and get used to.

One of the other great things about Cocos2d, is that it is easily compatible with Chipmunk, and Box2D. Although I personally haven’t messed around with Box2D yet, I know that Chipmunk is a fantastic physics engine. Chipmunk was easily capable of being integrated into the game I was making, and it helped me out greatly. Chipmunk is also open source, and C based. With Chipmunk, you can create gravity, a character, separating the limbs, and giving them their own mass.

I really recommend that anybody looking for a great game engine, to check out Cocos2d for iPhone, and Chipmunk.

Feel free to leave a comment, with a request (such as a tutorial) or a question!

Growing up in my time, from the early 90’s to now, I have always had a computer by my side. Computers have just been there for me, since the beginning, and I have never questioned their existence. Until now.

iWoz Cover

iWoz is the autobiography of Steve Wozniak, the creator of what we now call the computer. The book starts off from when Steve was a little kid, and continues through to modern day (or 2006). It goes through Steve’s childhood, and how amazingly gifted he was with electronics. With his father by his side throughout his life, Steve saw through the panels of flashing lights, and punch cards we used to call computers. iWoz is the true story of how Steve Wozniak went from computer geek, to cult icon. It explains how he invented the personal computer, co-founded Apple, and had fun doing it.

Now usually I don’t like to read, but this was an exception. This book was simply amazing. It filled the gap I had, with every tiny little detail. I really recommend this book for all. It is a great read for young, to learn about the past, and for old, to fill in the missing details.

Now, I don’t want to spoil the book for anyone, so here’s a link to get this FANTASTIC book, I truly recommend it.