DataMapper
Apologies for the massive delay between posts, it’s something I’m working on! I thought I’d do a very quick and simple post to explain something that I’d learnt recently, as we currently learning about databases. The example below assumes you would have created a database already using psql in your command line and stated that database in a separate datamapper_setup.rb file.
We are using Datamapper as our ORM (Object Relational Mapper), mainly because it’s quite easy to use (I’ve been told). Why can’t we just use Ruby to access our database stuff though? Because the database is written in SQL and it won’t understand what Ruby is asking of it. Enter Datamapper! All DataMapper does is basically allow us to call methods, using Ruby, to Create, Read, Update and Delete items in our database (CRUD — see other post).
class Books
include DataMapper::Resource
property :id, Serial
property :title, String
property :author, Stringend# Using DataMapper, you can now create a new book:user = User.create('DataMapper','Data')
This very simple example illustrates a Books table, that stores the title and author of a book. Using the ‘create’ method, we are able to create a new record, which is then saved in the database. DataMapper does all the SQL conversion for us, so when navigating to our database in the command line, we would see a new book called ‘DataMapper’, with an author ‘Data.’ ID is simply just a number referring to the row and it’s serialised and increments automatically every time a new entry is created.
Obviously it’s a lot more powerful than this, and has built in methods than can check the uniqueness of the the record, data type of the record and so on, it’s pretty cool and makes working with databases a lot easier!
If you’re interested, head over to the datamapper website to read and find out more.