-
2 weeks 2 days
-
2 weeks 4 days
-
4 weeks 2 days
-
4 weeks 5 days
-
4 weeks 6 days
Ruby's DataMapper 101
It's been a while and lately I've been playing again with Merb so I figured I'll post about DataMapper. DataMapper is an ORM for Ruby which most Merb users prefer because of its simplicity and neat features.
Before we can start playing with it, you can install it using the following command:
sudo gem install dm-core
Also, for the purpose of this tutorial we're going to install the sqlite database driver:
gem install do_sqlite3
Once installed, we can require it in our application and setup our database connection:
require 'rubygems' require 'dm-core' DataMapper.setup(:default, 'sqlite3://')
Then we can start defining our models, for this tutorial we're going to create a pizza recipe with ingredients:
class Pizza
include DataMapper::Resource
property :id, Serial
property :name, String
property :price, Float
has n, :ingredients
end
class Ingredient
include DataMapper::Resource
property :id, Serial
property :name, String
belongs_to :pizza
endAs you can see they're just regular classes except that we're using the DataMapper::Resource as mixin, this is what actually makes the class a DataMapper model. The properties define the fields and the "has n" and "belongs_to" defines the relationship between the two models. Here we're saying that a single pizza can have several ingredients and an ingredient belongs to pizzas.
Now that our models are all set, we can start migrating which actually constructs our database and tables.
DataMapper.auto_migrate!
Alternatively we could have just run Pizza.auto_migrate! and Ingredients.auto_migrate!, it works the same way. Now we can start playing with it, lets start by creating pizzas and ingredients:
pizza1 = Pizza.new(:name=>'Stupid pizza',:price=>3.0) pizza2 = Pizza.new(:name=>'Unknown pizza',:price=>2.5) ing1 = Ingredient.new(:name=>'Bacon') ing2 = Ingredient.new(:name=>'Ranch Sauce') ing3 = Ingredient.new(:name=>'Cheese') ing4 = Ingredient.new(:name=>'Pepperoni')
Creating a new record is as easy as creating a new instance of every model we've defined. Next we're going to asisgn which ingredient belongs to what pizza:
pizza1.ingredients << ing1 pizza1.ingredients << ing2 pizza2.ingredients << ing3 pizza2.ingredients << ing4 pizza1.save pizza2.save
We need to actually save them to persist the data by calling the save method of our model. We can now print all the pizzas and their ingredients:
pizzas = Pizza.all
pizzas.each do |pizza|
puts pizza.name + ' (' + pizza.price.to_s + ')'
pizza.ingredients.each do |ing|
puts ' ' + ing.name
end
end... and the output:
Stupid pizza (3.0) Bacon Ranch Sauce Unknown pizza (2.5) Cheese Pepperoni
Updating the record is as easy as editing the model object's field and calling the save method. For deleting you can also call the destroy! method, lets say we want to delete the Cheese ingredient we can do:
ing3.destroy!
Thats it! This is just a short introduction to DataMapper and there's more to it than just what I've showed you here, check their website for more info.

Facebook
Twitter
Post new comment