Coding 101 68 (Transcript)

Net casts you love from people you trust. This is Twit! Bandwidth for Coding 101 is provided by Cachefly at

This episode of Coding 101 is brought to you by Digital Ocean; simple and fast Cloud hosting built for developers. Deploy an SSD Cloud server in 55 seconds. Try it today for free. Visit and once you sign up be sure to enter the promo code c101 in the billing section for a $10 credit.

And by the online learning platform with over 3000 on demand video courses to help you strengthen your business, technology and creative skills. For a free 10 day trial visit That’s On this episode of Coding 101 we say goodbye to Carlos Souza in the last bit of Ruby on Rails.

Father Robert Ballecer: Hello and Welcome to Coding 101. It is Twit’s show for the code monkey and the code warrior. I’m Father Robert Ballecer and joining me as always is my super special non-guest permanent co-host… I actually kind of like that. That’s a good title – Mr. Lou Maresca from Microsoft. He is a senior lead developer. Lou thanks for coming back to Coding 101.

Lou Maresca: Thanks for having me again Padre.

Fr. Robert: Now we will jump into the last bit of Ruby on Rails with Carlos Souza. It’s actually been a really fun module. The last couple of weeks have been all about what Rails does to Ruby. I think it really helped our audience to understand what a framework does. It helps the language actually fulfill its promise and especially with Ruby on Rails. Since it builds all those dependencies for you to a lot of people in the group it has been like black magic.

Lou: That’s really what frameworks are supposed to be about right? Black magic – they’re the black boxes of the world where you hopefully make things easier for yourself by utilizing some other person’s work. I think that’s where Ruby comes in as a powerful language.

Fr. Robert: Right. There’s an item that you put into the dock that I think is very important because I have had some feedback from the episodes from people who were saying; “so all I have to do is let Rails do everything for me right”? Let’s set things straight. Rails is great, a framework is great. Any framework for any language is fantastic for helping you do the scud work and getting that out of the way so that you can have very consistent programs from 1 project to the next. However before you go into that there is some basic design that you do need to take care of. Do you want to talk a little bit about that Lou?

Lou: Absolutely. There’s a lot of principles out there today like how you can become a better programmer and how you can make your code more maintainable and more usable and where do you really start? I wanted to go over the basics – where do you start and what can you use from a design principle standpoint to really write really good code. There are several principles here. 1 of them is what we call the single responsibility principle and it’s just a bunch of fancy words to say that the idea is that the classes that you build in an object oriented language have only 1 responsibility. The thing is it’s really kind of hard to do because if you do that that means you have to break up your code in a way that maybe your classes might be this huge thing and they do a bunch of things and now you have to find out how to break it up. That’s why it’s always a good idea to start in the beginning and build a class that’s very small and lightweight and only does 1 thing.

Fr. Robert: You know Lou that’s actually 1 of those things that I think carries on no matter how advanced of a programmer you are. You’re always wanting to insert just that little bit more into that class because you don’t want to have to break it out into a brand new class. But as you said that’s proper design. We all know that from the very first class that we’ve taken on object oriented programming or class based programming. They tell us – sub divide the responsibilities. It just gets…it’s too easy to say well this is such a simple function I need to add in I’m just going to tag it onto this 1 that already exists.

Lou: That’s right, it’s that whole principle of KISS or keep it simple silly. That whole principle means keep your code simple, keep those classes doing 1 thing and 1 thing only. Then you move onto what they call the open and closed principle and that’s exactly what you just talked about. The open and closed principle is again just a fancy way in object oriented language to say allow a person to extend the functionality but closed to modification. Don’t modify that class to do something else but allow people to actually extend its functionality and do more things. Usually the whole principle is done by using inheritance in object oriented languages but again it can be done other ways as well. The idea is you can maybe modify your code to just fix bugs and fix errors but when you want it to do something else or add functionality to it; it should always be extended in some way. 

Fr. Robert: This becomes really important whenever you’re working in a group. Most of us if you’re going to go on to programming you’re going to have to work with others and if you’re constantly changing the class and adding functionality it may make sense to you but you may just have broken a dependency that someone had on your class. So as you said yes fix bugs and then extend. Don’t modify.

Lou: That’s right. That 1 kind of leads into the 3rd one which is what they call code duplication. What that means is a lot of people when they code they tend to maybe grab code from other people’s libraries or when they’re working with somebody or they might duplicate the code in several aspects. In that case you find yourself writing the same code over and over again in different spots. When that happens that becomes what they call an unimaginable nightmare – what they call the root of many evils for duplication. What you do is when you have to fix a bug you actually have to fix it in many different places and it just becomes really unmanageable. There’s an old thing that I used to do when I used to consult for programming. I used to have this deck of cards and every card would say abstract that. When somebody would come in front of me they would give me a problem and I’d blindly have them pick from the deck and they’d pull out a card and it would say abstract that. What they didn’t know is every card said that and the reason is abstraction leads into that single responsibility principle meaning that your functionality should be very simple and you should be able to pull that functionality out and create a class that only does that. And then that means that you don’t have to duplicate the code, you just have to use that class or that abstraction to be able to do the stuff that you need. When you find yourself duplicating code that means you haven’t abstracted your code or created that little block of code, that single responsibility class well enough and you need to look back and refactor your code so you can get it better.

Fr. Robert: Lou let me go to the other side here. How do you know when you’ve abstracted enough? Because you can keep breaking down a logic tree into its individual components and you might never stop. When do you say ok this is abstract enough?

Lou: There’s a point. If you’re building like an API then your abstraction can never be enough. It means that the smaller components that you can do people might want to be able to reuse those components. But when you’re building an application sometimes you know that this library for encryption is never going to be used anywhere else but the data pipeline so I’m just going to leave it inside of here as part of the functions but I’m not going to break it out into its own class so other people can use it. It is really at your discretion but again writing separate abstractions, writing separate classes for responsibility allows you to test things better as well. So there are many different testing frameworks in all the different languages from Java to C Sharp, C++ and they allow you to stub or mock out or fake a class or an object in a case and then it allows you to test that object. By abstracting it, it is easier to test and easier to maintain. So when you say is there a limitation you don’t want a class to just have 1 function and do only 1 thing and then you have millions of classes but in the same sense by abstracting you make it easier to maintain and easier to test.

Fr. Robert: We’ve got Gardener in the chatroom being a smart chat saying I see you are doing addition everywhere. Maybe you need to pull back to that step. But yes, I understand what you’re talking about. As a programmer you can never abstract too much. Keep going down until you find that foundational level and then go down a little bit more. Let’s move on from abstraction to error handling because that’s also something… it isn’t as persistent as we go on but especially when you’re starting off error handling is baffling.

Lou: That’s right. 1 of the things that you’ll find is when you’re coding, no matter what example if it is Ruby or Java is that you’re going to get errors. What I’m talking about is not only compiling error but run time error where the user goes and does something and let’s say that your code doesn’t handle it. Maybe they refreshed a page or clicked on something they shouldn’t have and the error gets thrown by the framework or by the browser or whatever kind of run time you’re using. The whole idea here is to be able to handle errors as best as possible so obviously you’ve got to do a bunch of testing to kind of bring those errors to the top so you can understand what they are. In the same sense when you don’t know what those errors are or you know that there might be errors there that you don’t know about always look to collect them and create what they call telemetry for it. Meaning send those errors out to yourself somehow. If you’re building a piece of software, let’s say a productivity piece of software, find a way to send those errors out to yourself; like for instance Windows has what they call a Watson service which means when you’re running an app and it has an error that it doesn’t handle correctly it actually sends that error over to Microsoft. Same thing with Google and with Chrome Browser, it sends telemetry data out. Do the same thing with your software. If you know an error, handle it in a way that you know is very graceful or send that information out – also be graceful to the user and let them know that there’s a problem but again send that information out so that you can learn by those errors and improve the software.

Fr. Robert: Lou we’ve been handling a lot of the design elements of good programming. In a second I want to get into actual style and then testing that design to see if it actually meets up to our standards but before we do that do you mind if we take a break just to talk about the 1st sponsor of this episode?

Lou: Absolutely.

Fr. Robert: Of course the 1st sponsor has to be Digital Ocean. Now what is Digital Ocean? Digital Ocean is a way to spin up your wraps, your services, your programming as soon as possible. Now in the old way – the old way was you’d either build bare steel, you’d go out and you’d actually buy a server and install the OS of your choice, you’d make sure that it had the services to run the programming that you actually wanted to run and you didn’t have to open it up to the outside world which of course meant you had to handle security. When we moved past that we started to actually rent servers. Maybe renting parts of servers. That’s part of the internet economy but we now want to evolve past even that. What if you had a way just to take your wrap, your service, your thing, encapsulate it in the OS of your choice and throw it up ready for production or testing? That’s exactly what Digital Ocean does. Whether you’re an experienced code warrior or just getting started you need flexible reliable and affordable hosting. Digital Ocean provides developers with droplets which are virtual private servers that can be customized and deployed quickly to host web sites, web apps, production applications, personal projects, virtual desktops and almost everything else that you can think of with full root access. Think about that folks, think about how hard it is for you to get that especially in today’s virtualized world. Now we’ve been using Digital Ocean here to stand up projects. Jeff Needles has been using it for mirror stats, I’ve been using it for a little project I’m running on the side and any time we do something with Coding 101 if we want to open it up either to a sandbox environment to break it or when we actually want to open it up to production and the real world, well we use Digital Ocean. It’s built for developers and it is used by over 400,000 of them including me. Deploy and configure your droplets via a streamlined control panel or a simple API. Again you can choose your OS. Do you want Ubuntu, do you want CentOS, do you want, fedora, CoreOS or freebieSD? You can do all of that and a 1 click install allows you to quickly deploy those common apps like Jango, Docker, Droople, Lamp, MediaWiki, Word Press, Ruby on Rails and more. All servers are built on hex core machines with dedicated ECC ram and raid SSD storage and the servers can have up to 20 CPUs, 64 gigabytes of memory and 640 gigabytes of SSD hard space. Now it is scalable which means it’s going to meet your demands. You can scale it from something that’s small and in a sandbox to something that’s going to be in production for the entire interwebs. You get full featured DNS management and an easy to manage domain panel and you can use dedicated IPs. They also give you web console access with HTML 5 plus SSH, SFTP and KMVMC for virtual desktops. They’ve got an extremely active community and this is 1 of the things that you really want to look for. You don’t just want a service that can meet your needs. You want a service that can meet the needs you don’t even know you have yet and that’s what the community does for you. It’s so easy to get started and you can deploy your SSDs Cloud server in as little as 55 seconds. I don’t know why you wouldn’t want to try it now. Digital Ocean has incredibly affordable and straightforward pricing. Servers starting at only $5 a month. There’s also hourly pricing available starting at less than a penny per hour. But we’re going to make it so that you can get started today and deploy an SSD Cloud server for free. That’s right folks, risk free, jump in and see if it’s right for you. Visit and create an account. Once you confirm your email and account information go to the billing section and enter the promo code c101 for a free $10 credit. That’s plenty to get started and explore what Digital Ocean can do for you. That’s and once you sign up enter the code c101 in the billing section for a $10 credit and we thank Digital Ocean for their support of Coding 101. Now Lou we’ve got our design features down. What about style, because style is important. We’ve been talking about style in this module – Ruby and Ruby on Rails because you do want to keep to a certain style. You do want to stay within the conventions. What’s the best practices for this?

Lou: There are a lot of best practices for this but there are some high level things that you should pay attention to. For 1 thing when you’re writing functions or methods you should name them correctly. Meaning naming things are very hard to do but like for instance when you have a class that does something you’ve got to make sure that those functions especially the public ones, the ones that other people use are named correctly. So what you want to do is make sure that you’re naming your functions, your methods based off of intent or their responsibility. Sometimes more verbosity is better so sometimes maybe you’ll have a really long function name or method name but it will help. Of course not only that but also add comments to that function. A lot of languages today have the ability to add comments but always naming a function name because a lot of people just use… in some of the IDs. So naming that function something that’s very… you can understand the responsibility of what it needs to do then the better. That’s really the number 1 thing is making sure you do that. That same thing goes for variable names too. So if you have some public variable names or even internal variable names make sure you’re naming them correctly and in their intent, what are they storing and what are they doing, are they global or not global. The name might be pretty verbose, pretty long so make sure that again you’re naming things correctly when you’re storing things.

Fr. Robert: I had a project when I was still in college where I did not do this. It actually worked quite well when I put it down and then I came back 6 months later to prepare it for the final and I had named like function a, function b, function c, variable a, variable b… yes don’t do that. It’s a pain in the butt. This is 1 of those things where you just have to train yourself to work through the pain knowing that there’s going to be a reward at the end. Use a lot of underscores, use a lot of periods. Get yourself those long function names so that you know all of these functions are a member of this grouping which is a member of this class and then everyone at a glance knows exactly what your class does.

Lou: You’re probably the millionth and one developer that I’ve talked to that has had that problem. I’ve had that problem, I’ll write code and I’ll write it with very horrible variable names, very short like using E for error in your code or something like that and then you go back later and you have no idea what it does.

Fr. Robert: Yes so use descriptive names for your functions and for your variables. But it’s not just the name right?

Lou: Obviously classes, that single responsibility principle says that your classes should just do 1 thing. That means that when you’re looking at a class it should be this massive thing on the page but it should be really simple. Have several small functions and be kind of small when it comes to code size. That also goes along with your function size. So your functions, your methods should only do very simple things meaning the shorter the better. That means it is easier to test, easier for people to understand when they’re looking at your code and again it becomes very easy to maintain later on. So again keeping your code small. I’m not just talking about what they can do or the responsibility, I’m talking about the actual code and making sure there’s not that much because again it becomes very hard to maintain it later on.

Fr. Robert: And of course the nemesis of every programmer…commenting.

Lou: That’s right. Making sure that your code is readable is 1 thing; making lots of code can sometimes be unreadable but also making sure that you’re commenting. When I started out writing code I used to go and look at other people’s code and I’d walk every line of that code and then every line I’d add a comment so I could understand what that line did. So over the years I’ve taken that same approach and some people would say I over comment. But when they read my code they definitely understand its intent and what it is responsible for. The reason why is I put a comment pretty much every other line saying this is what it is supposed to do. That way when it doesn’t do that they know that there’s something wrong with it. I think that’s the key – make sure you’re commenting your code. Every language has the ability to comment so making sure that you do it is very important.

Fr. Robert: 1 last bit and this is actually important because we’re going to be showing this off in the module today and that is testing your code. Testing against your code. Something like Ruby on Rails actually has that build in the framework which again is part of the black magic that is Rails. But not all frameworks and not all coding languages have that built into a nice eco-system. What would you suggest for best practices on testing code?

Lou: Like you said Ruby on Rails has the ability to generate a test right out of the box. A lot of other frameworks or other run times or other languages have the ability but really when you’re coding something no matter how complex it is what I like to do (and it is called test driven development) but there’s many other camps of this; what I normally do is I’ll write a test. I’ll have a list of things that I wrote out and say this is what this class, this code is supposed to do. This is the intent, I write it out on a little piece of paper and then I go and actually write tests that actually exercise that. So like for instance if I wanted something to translate a string to another language – the output is that it translates it correctly. So I’ll go write a test so that I expect the translation to be correct and then I’ll run that test. The thing is since I haven’t written the code yet it will fail and then as I write the code that test will start succeeding as it moves along. So I know that my original intent that I wrote down is being succeeded by the fact that my test is now succeeding. A lot of frame works have this ability and it is usually called unit tests meaning that you’re just creating a test for a specific unit in your code and it is doing 1 thing. What test your development means is that you write the code out front. You don’t actually start with the code. You write the test first and then it will fail up front and then as you start the code it will start to succeed and then you know you’ve done what you needed to do. Again a lot of frameworks don’t have that so like Visual Studio has it built in but a lot of these other frameworks don’t so sometimes you have to go and look. Java has JV unit but you have to go and find JV unit on the web as a separate framework. Depending on the framework you’ve got to go research it.

Fr. Robert: That might actually be a decent module for us to do – sort of a non-programming module where we get a developer like yourself and say ok I’m going to show you 10 different languages and I’m going to show you 10 different stress tests. Things that you can get easily running it against your code base to make sure that at least the basics are taken care of. That actually might be a good 2-3 episodes.

Lou: Yes absolutely.

Fr. Robert: We’ve actually got Dallas in the chat room who is saying have you ever had someone complain that you’ve had too much commenting because I’ve never made that complaint ever.

Lou: Some of the veteran programmers that are around today they will complain.

Fr. Robert: Really?

Lou: Yes but you’ve got to remember they used compilers and they simplify your code and they remove comments. There’s no reason why you shouldn’t have comments in your code other than the fact that maybe you don’t have a lot of storage to store the code but I don’t believe that. I’m not in that school.

Fr. Robert: Well Lou how about this, let’s go ahead and take 1 more moment to go ahead and thank the 2nd sponsor of this episode before we go on uninterrupted through episode 4 of our Ruby on Rails. Shall we do that, because I want to take everyone home and just let them bask in the glory that is that black box. Of course the 2nd sponsor has to be Now we talk about a lot here on the Twit TV network because we’re all about learning be it about Coding 101 or Know How or Twit. We’d like to let people learn at their own pace. We’d like to let them fill their knowledge holes with knowledge and that’s what does. is the 1 stop shop on the internet to get everything you need to know. Not just about programming, not just about computers but pretty much everything. Do you want to learn new business skills? Do you want to learn about Excel? Do you want to learn about something that’s going to help you in your hobby; well that’s what will do for you. It is for problem solvers, for the curious, for people who want to make things happen. Maybe you want to develop an app, learn a new programming language, master Excel or sharpen that Photo Shop skill; well has everything you need to feed your curious mind. Some of the latest Lynda courses that I can recommend because this is Coding 101 are Google App Engine Essential Training, Test driven development with Node.js and “Up and Running with PHP SimpleXML. I know I’ve talked about Code Clinic before; it’s a multi course series where experts look at common code challenges. This is a great way to learn and they offer their solutions using C++, C Sharp, Java. HP. Python and Ruby. Lou and I have talked about using the right language, the right tool for the problem and that’s what understand. Their experts get that they just want to give you tools for solutions. Now with a membership you can watch and learn from top experts who are passionate about teaching, you can stream thousands of video courses on demand and learn on your own schedule. You get to learn at your own pace which means courses are structured so that you can watch them from start to finish or consume them in bite size pieces. Any way that’s good for you is good for Lynda. You can take notes as you go and refer to them later and you can even browse transcripts so you can follow along or find that individual section, that small bit of knowledge that you need to fix a particular problem. That’s 1 of the things that make so useful. You can create and save playlists of courses that you like, you can share it with friends. In other words you can share knowledge and you can share an easy way to learn. Really this is what we’re all about and we thank for being part of it. Now with your membership you get unlimited access to training on hundreds of topics all for 1 flat rate. Whether you’re looking to become an expert, you’re passionate about a hobby or you just want to learn something new – and I know you do because you’re watching Coding 101 – I want you to visit That’s and sign up for your free 10 day trial. That’s and we thank for their support of Coding 101.

Hey Lou, you think we should get a little bit of Ruby?

Lou: I’m looking forward to it.

Fr. Robert: How about a little bit of Rails?

Lou: That too.

Fr. Robert: Let’s do that. Hey Zach, push that magic button. Thank you Lou and Padre. I’m back here on the sky desk with Carlos Souza from Code School. Carlos has been guiding us through Ruby on Rails the last few weeks and now he’s giving us a final lesson – how to use active record, how to hook up the model and your data base and how to profit by pushing your application live. Carlos Souza thank you very much for joining us again.

Carlos Souza: Thank you very much for having me. It’s a pleasure to be here.

Fr. Robert: In the last couple of weeks we’ve done a whirl wind tour through Ruby. We found out why you prefer the language, why it is so easy to learn, why it is so easy to program in. We’ve seen all the fantastic tricks of the trade with Rails and how it sets things up in dependencies so quickly and so automatically. But there is 1 last step and that is how do we push our application live, how do we make it ready for the profit. Take us through it.

Carlos: A couple of things that we’re going to do before we do that; the 1st thing that we’re going to do is make sure our server is running here. Let’s go back and run the root. If you remember from our 2nd episode the root of our application is still taking us to that welcome index. Let’s change it so that it takes us to the book index because it’s a reading list application. Let’s go back and open our routes file. In our routes file I’m going to change welcome index to books index. So books index is going to be a route. So remember if you want to learn more about routes file you can literally open the file and just read the comments that Rails added for us. So if you go back and hit root now we are defaulting to the listing of our books. That’s step 1. Before we push to production let’s create, let’s add an author to our game here. Basically a book has 1 author, an author has many books so that’s the association that we want to create. This look a little bit like this; we have our book model and we want to create our author model and the relationship between these 2 would be that an author has many books. So for each 1 author you can have many books. The way that we say that is that a book belongs to an author and an author has many books. Now remember these 2. We’re going to use them in our active models. 1st thing we do is create our author model. So instead of using a scaffold I’m just going to use the generator for model. Rails generator, model and author. The author is going to have 1 property or 1 data base attribute which is going to be name. I generated a model and as you can see created the migration file, our author model and our author tests. Now we’re going to run the migration and we haven’t created any views but if we jump into a Rails console we can see that we can query the data base for authors. We can create a new author, we can get the 1st author and we can delete our author. Empty, right? If we look at the models we have our author model which is empty and our book model which is empty except for that validation that we added. Now we need to be able to link 1 to the other. To do that we need to add 1 more thing; we need to add a foreign key to books because each book will need to know to whom it belong to. So to do that we’re going to need to make a change to the data base that is not part of any model. It’s not a change that we have done yet. It’s not part of the scaffolding and it’s not a part of creating a model. The way that we make changes to the data base in Rails is using something called migration. We’re going to have Rails generate a migration and we’re going to say add (this is another convention for Rails) we’re going to give it the foreign key name which is the name of the model followed by ID. So add author ID to books which is the name of the table. Then we’re going to say author ID is going to be an integer and it’s going to be an index. So this is going to create an index on the data base which basically makes it faster to do searches on this specific field so it’s always a good practice to create indexes for foreign key columns. It’s a data base practice that Rails allows us to follow just by adding an index like this. We’re going to run this migration and if we look at the migration now… let’s see what it looks like. You can see that it has an add column method that says add a column to books called author ID and the type is integer. On the following line it says edit index to the books in the author ID column. This is the Ruby code that Rails will translate into sequel. Let’s run this migration I just created. Ok we added the author ID column. Now if you want to look at the current state of our data base we’re going to look at this 1 file called schema.rb. This is what our data base looks like. It has the authors table with a name and then created at and updated at are sort of magic data base fields that Rails automatically manages for us. We know when each record was created and when it was last updated. We don’t have to worry about it from a development perspective, Rails takes care of it and it creates it automatically for us. Now the books table has a title and a description in the 2 fields and at the bottom it has the author ID which is the foreign key that it’ll use to associate a book with its author. Down at the bottom we create an index for that column. This is all Ruby and again Rails trail leads this to sequel. This is all the data base changes that we needed to do. Now let’s jump into our model. The 1st thing we’re going to do is jump into our book model and say that it belongs to an author. Now in our author model we’re going to say that “has many books”. That’s all we needed to do and these 2 methods connect these 2 models and it gives us a bunch of methods that we can call on their objects. Now let’s look at what those methods are. Fire up the console, let’s create an author just to make sure that we don’t have any…So dot count…just generates a sequel count. Let’s create a new author. Now it’s going to be…which books do we have?

Fr. Robert: We’ve got Harry Potter in there.

Carlos: And the Gun Slinger. Ok perfect. The 1st author is going to be J.K Rowling and the 2nd author is going to be Stephen King. Now what we’ll do – let me store these in variables. So we’ll call this Rowling, author first and then King author last. Now Rowling is a variable holding the JK Rowling record and King is holding Stephen King. Now let’s get our first book. We’ll call it book 1 and it is the Gun Slinger. The author of this book is Stephen King so I’m going to do book 1, author…let’s just call this method now and see what it does. It is “nil” so it says it does not have an author. Now if we do author ID it is nil. Notice …let me open it up here. App, model, book. Notice that all we have here belongs to author. We don’t have a setter or a getter for…explicit getters or setters for this property. If we go back and do author equals king and book now we can see that book 1 author is linked to Stephen King. So if you do book first – Gun Slinger, if we do author then it knows how to generate the proper sequel statement that pulls the author for that given book.

Fr. Robert: Of course it goes the other way around because that author has that book.

Carlos: Exactly. If we do king.books it is going to bring in all the books that belong to Stephen King.

Fr. Robert: This doesn’t look all that impressive because there’s only 2 books in there right now but as we were to add more and more books you’d see more and more why you’d want that relationship.

Carlos: Yes exactly. Now let’s show this from a web view. Let’s change our view to display the author name by the book. Let’s go here.  Right now all we have is a title of the book. Let’s go here, app, views, books, and index and next to the title let’s say we want to say by and then the name of the author. So book, author…let me break the line here just to make this easier to read. Let’s see what that looks like. Well it brought the author but it’s printing out the object reference.

Fr. Robert: Right.

Carlos: Ideally you want to say Stephen King which is the name of the author. So let’s go ahead and do “models, author” and override the 2 S methods on the model to return the name. Because essentially this is the method that’s being called on our view. So if we override this and tell it to return name instead now we have our author name.

Fr. Robert: Nice.

Carlos: Let me go ahead and also add to book last which is a Harry Potter – HP. So HP author right now is nil and I’m going to add Rowling and HP save. This is a console and I’m going to go back and re-render our web view and you can see that JK Rowling is now set as the author for Harry Potter.

Fr. Robert: Of course if we wanted to improve the interface a little bit we could make the author hot linked so you could see what books the author has.

Carlos: Yes, do you want to do that?

Fr. Robert: Yes, let’s do that.

Carlos: Let’s do that. Ok, the 1st thing we want to do is books, index, is at a link here. So we’re going to link to…do you remember the helper from our previous example? To… You’ll notice here we are using the author as both the string for the link and also the argument for where the link is going to be generated to. Let’s go ahead and just do that so we can see what the link is. So it’s saying that there’s an undefined method author path. What this means is that link to looks at the routes file to know which routes we have to work with. Right now our routes file knowns nothing about authors because when we created authors we created the model, we didn’t create the scaffold. So let’s go ahead and add “authors” to our routes file and refresh our page. Now we can see that the link is there. If we click here we don’t have the page yet but it’s taking us to the proper URL. So when we clicked on the 1st author it took us to /author/3 and when we click on the 2nd one it takes us to /authors/2. Let’s go ahead and create those…1st thing we have to create is a controller because that’s what’s saying error message and it’s saying uninitialized constant Authors controller. So again if you just follow the error messages you’re going to be able to figure out what you need to do. So vim, app, controller, authors controller and create authors controller. Inherit from application controller and create our show action. Which is the action that we need to implement because we’re showing a specific author. We’re not listing the authors. So in here we’re going to assign an author and we get this param on the routes, the params ID and we use that to find the author. If you remember from the console we did (not with author, I think we did with book) we can do find, 2 and that will return the object. It’s the same method we’re using here in the controller. So we’re finding an author and assigning to author. Another good practice is to never assign 2 variables in the view. So this is going to make sense in a second but what I’m going to do here is say books equals author.books. So now we have these 2 variables, instance variables that we can access in our authors view. Let’s go ahead and we might need to create the authors folder and inside of there create this html.erb and call this and let’s just give it the name of author. Let’s go ahead and render that and see if it works. It works, it has the name of the author. JK Rowling and Stephen King. Now let’s list their books. So pull an unordered list and now I’m going to loop. If you remember how we looped from our first episode we’re going to use each. Each do book…

Fr. Robert: It has an internal iterator that gets them…

Carlos: Exactly. Then do book title, list. So now if we render we can see the list of specific books of this author. Pick author and it lists the books. Let’s add another book. Let’s do…I know, any other book by her.

Fr. Robert: She does the thing of fanciful creatures. The non-Harry Potter book.

Lou: Just do another Harry Potter book.

Carlos: Alright. Book create title, the casual vacancy description “never read it”. Oh I forgot to add the author right so let’s go ahead and… the last command that we run on the terminal is always assigned to this magic variable underline. So I’m just going to say casual. See what I did here – underline, so I’m going to say casual author equals Rowling I think. Yes. So casual save. Now when we refresh you can see that it is there.

Fr. Robert: Nice, it is so simple isn’t it? You know Carlos I have to ask here because some people in the chat room were fretting that we were going to make that the homework. Could we make the homework harder for them? We have to it’s the last one. They have to have something that’s really going to challenge them. We’re still going to go over production but I’m going to break from tradition here and ask what the homework is as of now?

Carlos: That’s a great idea. Let’s make the homework… so if you notice we’re adding authors on the Rails console. So the homework will be to figure out how to add authors through the web interface.

Fr. Robert: There you go. Because the web interface was automatically created we need to see if you can alter it to do the same thing from the web interface that we just did from the console.

Carlos: Exactly and the 1 hint that I’m going to give you is that you’re going to need a drop down box, a select box.

Fr. Robert: And if you don’t know how to do that just look up any HTML primer and you’ll find it. It’s very simple and let me think…yes Carlos already gave you everything you’re going to need because he showed you how to pull params in order to make this work.

Carlos: Yes.

Fr. Robert: Alright, Carlos now that we’ve got the homework let’s go to the money shut because the money shut has to be how do I take this web application that I just created and make it rain all over my web server.

Carlos: Let’s do it. I’ve been using “git” so far just to commit different versions of this app so if we ever run into issues we can go back to previous versions and sort of debug our app. I’m going to go ahead and just commit the latest changes. So add author and relationships. Ok our git is clean. What we’re going to do is deploy our Rails applications to Heroku. If you don’t know Heroku you should definitely check it out., it’s a great platform as a service. What that means is that they have everything ready for you to deploy your Rails applications without having to worry about the infrastructure. I’m not being paid to say this, I’ve used them forever, I use them all the time, best way to get started.

Fr. Robert: Actually this is the future. This is what we talked about now. We’re where you package your application and you push it onto the infrastructure someplace else. This is not a secret, this isn’t advertising. This is your choice for doing just that.

Carlos: Yes absolutely and they not only support Rails. They support pretty much any other language out there. JS, python, go, PHP and whatever. This is what we’re going to use. I’ve been using it for years so I’ve already set up all my environment and I have an account there and everything. Which means that I have this heroku command on my terminal which is what we’re going to use to create our slug or our dino or our…instance of a server on heroku. Let’s create an app, heroku apps create. If we want to use a specific name we could use it as an argument, if not heroku just gives us a name and I always like to see how creative heroku can get. So heroku, apps create. Damp-spire 3408, so that’s the name of our app and if you look at the last message you’ll see that git remote heroku added. So if you look at the remote that we have here you’ll see that we have a heroku remote. What this means is that we’re going to use git to deploy our Rails application. Rails application deployment, our Rails application deployment will be nothing more than a git push to this heroku remote. That’s it. Now there are a couple things that we have to do to our Rails app to make it heroku ready. Let’s go over them real quick. The 1st 1 is to change the data base that we use in production. So far we have been using sequel lite 3 for all of our environments, for developments, for running our tests and it’s currently set up as production. But heroku doesn’t support sequel lite. Instead it uses a much, much, more powerful data base which is called pose risk – pose risk sequel. So what we do here is we give this sqlite gem a group of development and tests. This is another cool thing about Rails; it allows you to use different databases on different environments. For this one we’re going to use development and tests – sqlite3 and then…let me see if I have anything else here. The gem – pg. which is going to be for group production. So essentially what I’m saying is that when I run my application in development or tests I want it to use sqlite3 but when I use it in production I want to use pose risk. Now we don’t have to worry about data base credentials because the file that we use for data base credentials gets overwritten – gets replaced by heroku when we deploy our app. So the heroku injects and uses whatever credentials they need for their data base service. This is the file and you can see there is a development – a group here and then there’s also a test and a production but we’re not using production. So that is the 1st step. The next step is to add another gem called the Rails 12 factor. Now Rails 12 factor is a series of practices for deploying apps to the Cloud. So if you Google 12 factor app…the 12 factor app – There is a very cool article that does over each of the factors which are 12…not much surprise and why they’re important. So these factors or these practices were all bundled into this gem called Rails 12 factor so that we can use them in production. Also in production the gem, rails 12 factor group production. That’s what we need. Let’s now bundle our app because we changed our gem file and every time we change our gem file we need to bundle it again and create another gem file dot lock. Let’s add that, so get add. Then we can say ready for Heroku. Now our git stage is clean and just to point out we have the heroku remote that was added by the heroku app screen command and all we need to do is git push heroku which is a remote master which is the current branch that we’re running. Let’s see what that looks like. You can see that this is a git hook that they have over there, they already detected a Ruby app so they’re creating our server on the fly. They’re compiling Ruby on Rails, installing all the dependencies.

Fr. Robert: That’s actually pretty slick. I like that.

Lou: Yes it’s pretty cool.

Fr. Robert: And then it’s going to get to the data base part right towards the end here?

Carlos: Yes, well half through, it’s the next step. So it’s about to launch our app. Alright so it’s launched but we still need to run the migrations. The connection is there. It’s connecting to the data base but it doesn’t run the migrations automatically. So the same commands that we ran locally, the rake commands we can run them on heroku by simply using heroku run, rake db migrate. When we run this we’re going to run rake migrate on our heroku instance.

Fr. Robert: Right so it’s going to take everything out of our lite data base and push it into theirs.

Carlos: No it’s not going to take anything out of our data base. It’s going to run the migrations on our app.

Fr. Robert: Oh ok I got it.

Carlos: You can see here the commands that I ran and then the output. Now if we want to open our app we do heroku open.

Fr. Robert: I had never seen that before. That’s nice and slick.

Carlos: Yes. So our app is there but there’s no data in there right. So let’s just create 1 thing. Remember we ran the Rails console when we were local right? Let’s do the same thing now but on Heroku. So heroku run rails console. It takes a little bit more time. Now author create name, JK Rowling, JK, books create, title Harry Potter, description a wizard. Now if you refresh you can see it’s there. In production so…

Fr. Robert: Open to the world. Now all you’ve got to do is sit back and just wait for the money to roll in.

Carlos: Absolutely, that’s exactly what happens.

Fr. Robert: That’s what I’ve heard that once you publish your 1st app someone just shows up with a big bag of cash.

Carlos: Yes, I hear someone knocking on the door right now so it might be that.

Fr. Robert: Carlos this has been amazing. 4 episodes and you brought us from not knowing anything about Ruby to being astonished at Rails to being able to actually push out an app on an app deployment service. This is fantastic. Thank you so very much for being here. You could have done so much more with your free time so thank you for spending it with the Twit TV army. Could you please tell the people where they can find you? Of course it’s code school, of course it’s the place that you go on the internet if you want to learn how to build a web app or if you want to learn how to do development. It’s the place where you go if you want to learn just how this programming thing works. If you want just a little inkling as to the world that you live in. But you are located in many places. Could you please tell them where they could find Carlos Souza?

Carlos: Yes absolutely. If you want to look at some of the code that I participate in, either 1 of my projects or 1 of the open source contributions that I have go to my github page. and if you want to follow me on Twitter it is the same handle. and I also help co-host a couple of different podcasts. There’s the Ruby 5 podcast which is a fast paced, latest news, 5 minute pod cast on the latest and greatest on the Ruby and Rails community. Check it out; You can also subscribe to it. If you’re more of a Java Script developer there’s another podcast that I also participate in which is called 5 minutes of Java Script. Same type of style, 5 minutes, we don’t want to waste your time. We’re going to give you the latest news in Java Script community. Yes, lastly Code School; if you want to learn more about Ruby on Rails, Java Script, Git, GitHub, HTML, CSS or any of the major frameworks as well – Ember, backbone, check it out at and I want to thank you Padre and everybody on Twit. It’s been a pleasure participating in this. This is a lot of fun. I enjoy this and if you ever want me back just drop us a line and I’ll be more than happy to come back here.

Fr. Robert: Oh absolutely. You know we’re going to ask for you back. But you know what, let’s do 1 more thing. You’ve got to throw another plug in for Rails for Zombies because seriously that is a lot of fun.

Carlos: Ok. Yes absolutely. So if you want to learn more about Rails but you’re not sure if it’s your thing or not and you don’t want to spend countless hours trying to install it locally before you can start using it go to This is what it looks like. It’s an interactive browser based tutorial so again you don’t have to install anything in your computer. You’re going to watch a couple of very short videos, very fun; we try to make them as engaging and fun as possible and then after each video you’re given a couple of challenges about those specific subjects that we just taught. So this is 1 example using the Rails console. Again this is all in the browser so you’re trying to find a zombie with ID 1. This is easy – zombie.find.1. Congratulations you got it right. That’s what they course looks like. It’s about 6 levels and we teach you all the most important aspects and components of Rails. It’s kind of like what we’ve done here in these 4 episodes but in the browser you can do it again and after you’re done you can decide whether or not Rails is a good fit for you.

Fr. Robert: Again Carlos Souza thank you again for being our code warrior. Sir we are forever indebted to you and we salute you.

Carlos: Thank you very much. See you.

Fr. Robert: Back to you Padre and Lou back on the code desk. I’ve got to say I had so much fun with Carlos Souza. I can’t give this man enough props. He came on, he gave us a topic that a lot of people weren’t sure that they wanted to learn and I got to say from the comments that we’ve had in the group, from the Tweets, from the emails I’ve gotten I think we’ve made some converts to Ruby and Ruby on Rails.

Lou: Yes he did a fantastic job and had some great examples too.

Fr. Robert: You know what, that whole idea of having a framework, an IDE that just kind of bugs you to test your code – I like that. That’s the kind of thing that you want to have happen in any sort of framework so that the code that you’re developing isn’t going to be stilted by something that you did half a year ago.

Lou: Absolutely. When you can specify your intent upfront and be able to verify as you move along that’s always being a better coder and a better programmer in the end.

Fr. Robert: Yes. Now Lou we are actually off next week because of Memorial Day here in the United States but when we come back we’ve got 3 straight weeks of wild card episodes. The reason we have 3 is because we kind of cheated out a little bit on the last module. We started the last module early because we kind of wanted to get into it. So we’re going to give you 3 this time and we’re going to start with Peggy Fisher who is a master of Stems. That’s Science, Technology, Engineering and Math. We want to bring her unique perspective to Coding, to programming specifically to reaching the coding world to those who might otherwise be excluded. But of course I want to thank you Lou Maresca as our super special permanent co-host. You know what I’m just going to keep extending it. Your title is going to get longer and long each week. You really bring so much to the show and I’m so happy to have you as my co-host. Could you please tell the folks at home where they can find you and your work?

Lou: Absolutely. You can always find me on Twitter. I’m LouMM and of course all my work that I do during my day job is found at

Fr. Robert: Don’t forget that you can find Lou here on the Twit TV network. He does guest appearances on This Week in Enterprise Tech. I’m also trying to get him on a Know How and Before You Buy. That’s coming up very soon so you’re going to see more of Mr. Maresca. Don’t forget that you can always get all of our back episodes. We know that these modules kind of lend themselves to being downloaded so you can watch them and re-watch them at your leisure. Just go to our show page at or coding 101, it all goes to the same place. There you’ll be able to download full episodes in any format that you want. If you want it in audio format, a video format or a high definition video format in case you want to follow us at home. Not only that but you can subscribe so that every episode automatically gets dropped into your device of choice in the format of choice. If you want it in your iPhone, your iPod, your iPad, your Android tablet, your Mac or PC or laptop or desktop we’ve got a selection for you because we love you. Also don’t forget that we have “show notes” available. There’s been somewhat of a glitch but we’ll get it all sorted out. I know I promise this a lot but we’re not having a whole lot of luck with this. At some point I may have to have Lou Maresca take a look and see what we’re doing wrong. But just again go to if you want to get the assets so you can follow along in each module. Also don’t forget that we do this show live. Or mostly live every Monday at 2:30 PM Pacific Time. Just go to and as long as you’re watching live why not jump into the chat room. I’ve got you right down there. There’s a little screen that has the chat room scrolling so that if you have a question, if you have an answer, if you have a comment that you think should go on the air it is a really good way to involve yourself in experimenting. That is Twit TV. Finally don’t forget that you can find me at Twitter. If you follow me you’ll find out who our guests will be each week, you’ll find out topics and you’ll be able to suggest future guests for Coding 101. In fact the last 3 guests all came from your suggestions so if you want to contribute, if you want to be a part of Coding 101, follow me at Wait, 1 last note. I want to thank my super TD because along with Lisa and Leo who let me do this show he is the man most responsible for making this happen. That’s right it’s Eskimo Zach. Zach could you please tell the folks at home where they can find you?

Zach: Thank you Padre. I’ve got to get my mic on. Yes you can find me on Twitter at Eskimozach.

Fr. Robert: Until next time I’m father Robert Ballecer. That guy back there is Lou Maresca. This has been Coding 101. End of line!

All Transcripts posts