Pastie now auto-senses if line-wrap is a bad or good idea. Feedback?
## mark a section (Learn more)
This paste will be private.
## Rails ActiveRecord class Playlist serialize :items_ids def videos(offset = nil, limit = nil) ids = item_ids || [] vs = Video.find(:all, :conditions => ["id in (?)", ids], :limit => limit, :offset => offset) hvs = Hash[*(vs.map{|v| [v.id.to_i, v] }.flatten)] ids.map{|i| hvs[i.to_i] } end def add_item(v) self.items_id ||= [] items_id.unshift(v.id) v.playlist_id = self.id v.save! end end list = Playlist.new list.add_item(some_video) list.videos(0, 2) #=> [ #<Video ...> ] ## StrokeDB Playlist = Meta.new do on_initialize do |doc| doc["videos"] ||= [] end end list = Playlist.new list.videos << some_video # store reference list.videos[0, 2] #=> [ #<Video ...> ] (auto dereferencing)
From the Design Piracy series on my blog: