Created by: James Golick and Jonathan Palardy for FetLife #NSFW.
Inspired by How FriendFeed uses MySQL to store schema-less data.
Install the gem.
sudo gem install friendly
If you're on rails, add this in environment.rb:
config.gem "friendly"
and create a config/friendly.yml:
development:
:adapter: "mysql"
:host: "localhost"
:user: "root"
:password: "swordfish"
:database: "friendly_development"
not using Rails:
Friendly.configure :adapter => "mysql",
:host => "localhost",
:user => "root",
:password => "swordfish",
:database => "playing_with_friendly"
Fire up your vim:
class BlogPost
include Friendly::Document
attribute :author, String
attribute :title, String
attribute :body, String
end
From irb or script/console:
Friendly.create_tables!
Just add the indexes you want to your model.
class BlogPost
include Friendly::Document
attribute :author, String
attribute :title, String
attribute :body, String
indexes :author
indexes :created_at
end
Run create_tables again and the index tables will be created for you.
Friendly.create_tables!
With familiar ActiveRecord syntax:
BlogPost.create :author => "James Golick",
:title => "Friendly has familiar syntax.",
:body => "So, there's very little learning curve."
All posts by James Golick:
BlogPost.all(:author => "James Golick")
Most recent posts:
BlogPost.all(:order! => :created_at.desc)
Install the memcached gem:
sudo gem install memcached
Configure Friendly to cache:
Friendly.cache = Friendly::Memcached.new(Memcached.new)
Configure your model to cache:
class BlogPost
include Friendly::Document
attribute :author, String
attribute :title, String
attribute :body, String
indexes :author
indexes :created_at
caches_by :id
end
And now your app is that much friendlier!