You already know how to build a basic web application with the Ruby on Rails framework. Perhaps, you have even taken Course 1, "Ruby on Rails: An Introduction" (we highly recommend it) where you relied on external web services to be your “data layer”. But in the back of your mind, you always knew that there would come a time when you would need to roll up your sleeves and learn SQL to be able to interact with your own relational database (RDBMS). But there is an easier way to get started with SQL using the Active Record Object/Relational (ORM) framework. In this course, we will be able to use the Ruby language and the Active Record ORM framework to automate interactions with the database to quickly build the application we want.
In Rails with Active Record and Action Pack, we will explore how to interact with relational databases by using Active Record, a Ruby gem, which Rails uses by default for database access. We will then take a look at what role Active Record plays in the overall request-response cycle, when a client (the browser) requests data from the server, as well as how to submit the data to the server. Of course, when accessing data, security is of paramount importance! We will talk about vulnerabilities such as SQL injection, as well as how to secure access to data by authenticating and authorizing users accessing the data. Take this course to build a Ruby on Rails application with Active Record to automate the detailed SQL interactions with our database.

YV

This course continues the Introduction with the same test-driven approach and delicious intensity of the material. Efforts to finish the course will be greatly rewarded.

SJ

Jun 05, 2016

Filled StarFilled StarFilled StarFilled StarFilled Star

I really liked writing the authentication myself. I've relied on Devise (and will continue using it mostly) but really got an appreciation for what was going on.

À partir de la leçon

Introduction to Active Record

In this module, we will begin exploring the database-interaction portion of Rails. We will start off with migrations that enable you to create and modify the schema of the database. We will then move on to discussing the Active Record gem Rails uses, which enables you to create, retrieve, update, and delete the data from the database. Before looking at Active Record, we will talk about some advanced Ruby features of meta-programming that will help facilitate our Active Record journey.

Enseigné par

Kalman Hazins

Adjunct Professor, Graduate Computer Science

Transcription

[MUSIC] Hi. Welcome to lecture six. In this lecture we'll talk about how to define the methods dynamically. So this feature is called "Dynamic Method" in metaprogramming. And basically, the idea is not only can you call methods dynamically with a send method, which is called dynamic dispatch, as we discussed in the previous lecture. You can also define methods dynamically. How do you do that? You have a define method, you pass it a symbol for the method name. And then a block for the behavior of matter. And this creates and instance method for the class. So let's see and example where the class, called whatever, and then we could have a define method. That we call symbol make it up and the behavior of what it should do, it should print out whatever. So we'll create the class, the instance of the class whatever and then we will call make it up and where make it up becomes a regular method of this whatever class, so on to this knowledge, let's see what we could do with our reporting system. So our reporting system, if you remember from a previous lecture, is basically the system that initializes the store and then has all these many methods that basically delegate the store method. So for example, get piano description, store, get piano description and so on. So what we could do now is, using the feature we just learned, we could instantiate the store and here's what we could do. We could grep the methods of the store instance, so store method's just an array. And the array has a grep method, which lets you pass in a regular expression. Which basically means that you can search for something very specific. In this case, we're searching for a pattern, ^get _{something in the middle}_desc, for example. And we are extracting whatever is in this, parenthesis and if you get lost in the regular expression syntax, don't worry about it, this is just to illustrate the concept. You could pass an irregular expression to the graft method. It basically let's you extract the product name, so piano, violin and so on. And then we could have a define report methods for method, as part of our reporting system which is our class, and we could define what each one of those extracted product names, we could define a method that says define method. Whatever the name is, so the name would be, for example get, piano and description, or get piano price. And the way we define it is by specifying store.send, which is using dynamic dispatch, get piano description or get piano price for each one of the products. So it turns out that this is all the code we have to write for our reporting system. We'll just look at the store, we'll look at the method of the store has, that match a certain pattern, and then based on that pattern we're able to generate more methods, and these methods, these get piano desk, or get piano price and get vital import desk and get item price, they become full fledged methods of this reporting system class, and now you can use them as you would use them before when you actually had to write all these methods for the get, piano desk and get, piano price yourself. It's pretty amazing. So there's no more duplication. You don't have to write all those repetitive methods over and over again. Not only that you get a bonus. If someone adds a new item to the store class your reporting system already quote unquote knows about it as long as the same method naming pattern is adhered to. So you have get a knife or get a desk or get a table. Whatever items the store sells your reporting system doesn't need to change. You don't have to add any more code. You didn't write much code in the first place, but you don't have to add any code at all because the system already knows about it because you're just examining the patterns that the store class has, and you're generating methods based on those patterns. So in summary defining methods dynamically can dramatically reduce the amount of code that needs to be written. And think about it this way. Let's see you have a ruby class that is our presentation of a database tables, columns in it. So, whatever code you're gonna write in your class, even if certain columns get added to a table later on, your code will quote unquote know about those columns. How cool is that? Next we'll talk about ghost methods and what those are.