I recently had to create a timeline-like feature for the project I am working on. We have two models in the timeline right now, Event (sorted by starts_at) and Photo (sorted by created_at). They are fetched like this in our controller:

@user = User.find(params[:id])
@photos = User.photos.order('created_at ASC')
@events = User.events.order('starts_at ASC')

We can easily combine @photos and @events into one array by using the + operator, like so:

@timeline_items = (@photos + @events)

But that doesn’t give us a sorted array (actually it is still sorted, but in this case @events appears after @photos in the array). We can use the nifty .order_by(&:column) to sort this array. If we wanted both models to be sorted by their created_at columns we can do this:

@timeline_items = (@photos + @events).sort_by(&:created_at)

Unfortunately we do not sort @events by created_at. What we can do is create an artificial attribute on each column, which I will call timeline_order_column:

# Photos
def timeline_order_column
  created_at
end

# Events 
def timeline_order_column
  starts_at
end

And finally we sort them like we wanted to:

@timeline_items = (@photos + @events).sort_by(&:timeline_order_column)
Advertisements

I’ve seen some confusion regarding the mysql2 gem in conjunction with Rails 3. The solution is really simple:

Gemfile

gem 'mysql2', '~> 0.3.6'

config/database.yml

environment:
  adapter: mysql2
  database: yourdb
  username: youruser
  password: yourpass

Couldn’t finish this one so instead of throwing it away I decided to upload it. It turned pretty OK but the ending is very sudden and doesn’t make much sense. ┬áHere it is:

By the way, I uploading everything to my SoundCloud account. If you like my songs you can download them there, ’tis free.

Having trouble getting your Paperclip processors recognized? Getting `uninitialized constant` errors? Place your processors in config/initializers/processorname.rb instead of lib/paperclip_processors

Can’t believe it took me 1.5 hours to find that one. My Google-Fu needs improvement.

Update:

As of 4e4f427 this issue has been fixed.

A super simple little nifty argument to bundler to skip installing gems for production. On my development machine (currently my Macbook) I don’t have MySQL set up nor do I feel the need to right now. Maybe when the project goes live, becomes more complex or I need to start optimizing I’ll go for MySQL instead of SQLite3. So my Gemfile is basically this:

source 'http://rubygems.org'

gem 'rails', '3.1.0.rc4'

# Asset template engines
gem 'sass-rails', "~> 3.1.0.rc"
gem 'coffee-script'
gem 'uglifier'
gem 'jquery-rails'

group :test do
	gem 'sqlite3'
	gem 'rspec', '~> 2.4'
	gem 'rspec-rails'
end

group :development do
	gem 'sqlite3'
	gem 'rspec', "~> 2.4"
end

group :production do
	gem 'mysql'
end

Running bundle install without the proper MySQL-stuff installed we’ll get an error, so how do we skip the production gems?


bundle install --without production

Ever since August 2010 I’ve been an avid guitar player. Not only have my enthusiasm for the instrument made me forget about all my prejudices about certain beliefs, it’s made me happy! </rabble>

Three days ago I happened to play some random notes with a certain amount of echo. These notes became a riff and pretty much instantly became the corner stone for a whole composition. After loosing some guitar tracks and re-recording them I’ve ended up with a three minutes long track.

The song contains no bass line as I don’t own a bass and the drum track is just a loop that keeps on going, drums are silent when I in reality would want a half tempo drum beat. Without further ado, here’s La vie: