MVC(Model, View, Controller)

In the next 3 chapters im going dive into what makes Ruby on Rails special, the MVC(Model, View , Controller) concept.

The M(odel)

it’s where we define our noun, e.g. a collection of cars have a list of attributes like name, weight, color, fabricate etc. It defines our stuff. It’s a description of what it is and what we can do with it.

To create a Model we type: ( in the home directory of our app )

rails generate model User name:string age:integer

# here we use the “generate” command to guess what, generate the Model with the name of User and with the attributes of name:string and age:integer. 

String = all sort of text, contains of letters and numbers. A sentence like “Hello World!” is a string and often enclosed by ” .

Integer = Numbers like 0 – 10.

The generation makes a migration file and a definition of the Model in our app. To add the migration to our database we have to run:

rake db:migrate #or

rails db:migrate 

Migration = It’s an instruction to update our database table

To define our User Model we can edit our user.rb file with basic methods like:

class User < ActiveRecord::Base #the class of User inherits from ActiveRecord Library

           def how_old #defining our method how_old to check how old the User is

“I’m ” + self.age.to_s + ‘years old’ #making a string

           end #ending our block of code


If we enter the Rails console by opening the command line and typing:

rails console

we can first make a new User with:

Mark = “Mark” , age: ’26’)

and call the newly made method: how_old on it with:


and it will print out:

“I’m 26 years old”


This is just a quick example of how to create and define a model. Theres alot more to it, which im going to talk about soon.




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s