In my previous post I wrote about the steps to creating your first route, controller and view in Ruby on Rails. Now I will talk about migrating the database and using models.
In your app, you will most likely want to use a database for something – either for inventory, user information, object information (like a database about movies or something). In this example, let’s say we’re building on our books app from the last example. What we will want to do first, in our terminal, is to create a database schema.
Step 1: $ rails generate model book title:string description:text author:string
In this one line we are generating a new book model, which has three attributes – a title, a description, and an author, and we are specifying what kind of information those attributes will hold. (note you could say title without string, because the default is string).
This will create a few new files for you, including a database migration file in db/migrate, a book model in app/models and some testing files.
In db/migrate you can open up your migration file, and you should see a new class called CreateBooks with a method called ‘change’ which creates a table and the columns should be the attributes that you entered in the command line. If you forgot an attribute, or want to change one, you can do it in this file now.
In app/models you will see a Book model, which includes just a class called Book.
Step 2: $ rake db:create
Now, if your console says this already exists, that’s cool, now you go to step 3
Step 3: rake db:migrate
Now your table has been made in the database, and you can start populating it with information. How we did this to start out with was to start the rails console and enter them in there. Rails console is a Ruby environment you can use from your terminal.
Step 4: $ rails console
Note that to get out this you can just type quit.
In the rails console you can then add new things to your database, using commands like this:
- s = shirt.new
- s.name = “daring fireball”
- s = Shirt.find(1)
Finding where id =1, this returns an instance
- shirts = Shirt.where(:name => “xxxxx”)
returns an array of shirts
- Shirt.where(“name like ?”, search)
- s = Shirt.find(1)
- s.name = “ xxx”
VERY IMPORTANT: Note that nothing goes to the database unless you use the save command.
So I might do something like this
Step 5: Add some data to your database through the Rails console (if you are a beginner anyway)
book = Book.new
book.title = "Alice in Wonderland"
book.description = "Some crazy stuff going on"
book.author = "Lewis Carroll"
Now, when you have created some books and saved them, then you should be able to refer to them within your rails app, by referring to the model. First you need to initialise the new Books model class in your welcome controller
Step 6: In WelcomeController’s index method enter
@books = Book.all
Which will initialise your books class variable, and pull all your books out of the database so you can use them.
Now to see your books, you can pull them into your index.html.erb view
Step 7: In welcome/index.html.erb enter something like
<% @books.each do |book| %>
<%= book.title %>
<%= book.description %>
Author: <%= book.author %>
<% end %>
If you refresh your browser (make sure you have saved all files and turned the server on with $rails server) – you should see your books listed!
Now we learnt a whole lot of other stuff about routes and things after this – but I think this is enough to get my head around for now.