Tuesday, July 13, 2010

Mongomapper with Rails 3

These are some notes I have taken while creating my first Rails 3 application with MongoDB/Mongomapper as the back end.

After installing RVM, Ruby 1.9.2dev, and Rails 3.0.0.beta4, I followed the instructions here to update a bunch of files (assuming you are running MongoDB locally):

config/initializers/mongo.rb
MongoMapper.connection = Mongo::Connection.new('localhost', 27017)
MongoMapper.database = "#myapp-#{Rails.env}"

if defined?(PhusionPassenger)
PhusionPassenger.on_event(:starting_worker_process) do |forked|
MongoMapper.connection.connect_to_master if forked
end
end


Gemfile:
source 'http://rubygems.org'

gem 'rails', '3.0.0.beta4'

gem 'mongo_mapper'
gem 'rails3-generators'


config/application.rb
(only first 8 lines shown, not change to the rest of the file)
require File.expand_path('../boot', __FILE__)

#require "rails/all"

require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "rails/test_unit/railtie"


Then, I went on to create a model, and to do that I used kristianmandrup's rails3-generators gem:
sudo gem install rails3-generators
rails generate model Book --skip-migration --orm=mongomapper

Edit this model to contain at least one MongoDB key:
class Book
include MongoMapper::Document

key :title, String
key :author, String

timestamps!

# Validations :::::::::::::::::::::::::::::::::::::::::::::::::::::
# validates_presence_of :attribute

# Assocations :::::::::::::::::::::::::::::::::::::::::::::::::::::
# belongs_to :model
# many :model
# one :model

# Callbacks :::::::::::::::::::::::::::::::::::::::::::::::::::::::
# before_create :your_model_method
# after_create :your_model_method
# before_update :your_model_method

end

We'll need a controller to access the Book model:
rails generate controller Book


This creates an empty controller file. Edit book_controller.rb to look like this:
class BookController < ApplicationController
def index
@books = Book.all
end
end


Create a view for this controller and action in app/views/book/index.erb:
<% if notice %>
<p id="notice"><%= notice %></p>
<% end %>
<h1>Books</h1>
<% @books.each do |book| %>
<div class="entry">
<h3><%= book.title %></h3>
</div>
<% end %>

Add the Book routes in the routes.rb file:

Inspector::Application.routes.draw do |map|
resources :books
end

Start the server to see if the application works so far:
rails server

Navigate your browser to http://localhost:3000/books/index. If you get a blank page with "Books" only displayed, then you are doing well, no worries. There are no errors, but also there are no records to display. The easiest/quickest way to add some books at this point is to use the console. Type "rails console", and add a couple of books:

book1 = Book.create(:title => "title 1", :author => "author 1")
=> true
book2 = Book.create(:title => "title 2", :author => "author 2")
=> true


Finally, make sure the rails server is running, and try to access http://localhost:3000/books. If all went well, the two entries we just made will be listed.

PS. Good resources on this subject:
http://www.mongodb.org/display/DOCS/MongoDB+Data+Modeling+and+Rails
http://blog.bitzesty.com/mongodb-with-mongomapper-and-ruby-on-rails

Saturday, June 12, 2010

MacRuby and CoreData reading and writing

I am working on an iPhone project that uses CoreData. We need to pre-populate the database with a large dataset, for which the data is available on a number of CSV file.

I decided to use MacRuby 0.6 to build a tool to get the data out of the CVSs and into the CoreData's sqlite3 database. Although MacRuby (an implementation of Ruby 1.9 for Mac OS X) has really good APIs for CSV manipulation (a reason I decided to use MacRuby instead of Cocoa/Objective-C), the documentation on how to use CoreData with MacRuby was very thin, to say the least. But I did find some example code that got me started.

The full code is available on Github. But here's a couple of things I learned along the way.
  • Once you have a data model created on XCode, you need to compile it into a ".mom" file so that the Persistent Store Coordinator can be instantiated with it. This can be done with momc tool that comes with XCode available in /Developer/usr/bin (see gist example).
  • The relationship property names are used when you want to retrieve records, and the attribute property names for saving data.
  • In a one-many relationship, the object returned is an Array, through which you can iterate in Ruby with the "each" iterator.
  • In a one-one relationship, just use the entity name and its attribute property name.
This is the entity relationship diagram I used in the Github example:

Friday, March 26, 2010

David Heinemeier Hansson at Startup School 08

This is a really insightful talk by "DHH", creator of Ruby on Rails on creating a great startup company and keeping it focused on what matters. Some of the ideas he talked about remind me of the "7 day weekend" by Ricardo Semler.



&amp;amp;amp;lt;div&amp;amp;amp;gt;&amp;amp;amp;lt;a href='http://www.omnisio.com'&amp;amp;amp;gt;Share and annotate your videos&amp;amp;amp;lt;/a&amp;amp;amp;gt; with Omnisio!&amp;amp;amp;lt;/div&amp;amp;amp;gt;

Monday, March 15, 2010

New concept laptop design: rollable laptop

Check this out, a laptop design where the computer rolls into a tube, based on flexible organic LED technology (OLED). The whole top surface of this computer is the screen, and a keyboard or tap controls appear when needed.

Monday, March 8, 2010

Microsoft Courier, an imressive digital journal device

Microsoft has demonstrated a very impressive digital journal device called "Courier". Its a dual-screen device that when closed is of the size of a 5x7 photo, and very thin. It supports finger gestures, like the iPad, but also includes a stylus for writing and sketching. I saw the demo video produced by Engadget, and I was very impressed. It should be released later (towards the end) of this year; I can't wait to see how it compares with the Apple iPad.

Thursday, February 25, 2010

How did it all start?

I came across this cartoon while surfing. I don't know its original location, but was too good to not include here.

Wednesday, February 17, 2010

The future of magazines?

Some have looked at tablet computers as the ideal format for the magazine of the future. Wired has done some custom design of puting its content on tablet computers. The result is really interesting. Check it out:

http://link.brightcove.com/services/player/bcpid56328629001?bclid=10175001001&bctid=66775419001