Master Ruby on Rails Active Record Basics In 60 Minutes | OdinSchool

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome to ruby on rails course we are talking about ruby on rails topics one by one in this video we are going to talk about your topic called active record that is nothing but the model before getting into the video why we are learning ruby on rails ruby on rails is one of the open source framework it is used to create your web applications very quickly it is also called as full stack framework it is used to create data based back to web applications it is following the concept called mvc that is nothing but model view and controller model is denoting your friend backend database view is denoting your front end and controller is the middleman so using ruby on ranks you can able to create your web applications very quickly so with this introduction let me introduce myself i am krishnaveri having 14 years of experience in this ruby on rails field especially in web application development software development and training i have trained more than 800 students online i am providing this latest syllabus to meet the industry standards i am working with audin school this audience school is a technology app skilling platform we help you to continuously accurate new skills so stay skilled and stay ahead with earth in school in this video we are going to talk about the basics of active record migrations and validations what do you mean by active record active record is nothing but it's one kind of component which comes with rates by default that will use the uh support the backend database concepts so active record is a dedicated component in rails to deal with the back end database uh migrations and then schema files etc so this active record is comes with rails as a inbuilt configuration so it is an inbuilt component which will support your backend database needs so it is it is working according to the concept called orm so this may be new to you orm concept active record is working according to the concept or principle called orm so we are going to talk about that orm is nothing but object relational mapping we are using a object oriented programming language that is called ruby in rails so rails framework is written in ruby programming language you already know that so we are using ruby so the backend database we are going to use here is sqlite3 so how this ruby coding is going to be mapped with this particular backend database for writing your scripts for example we are using sqlite3 how this particular object relation object oriented programming language is going to be mapped with this database usually table based databases uh if you are storing your data in tabular column format that database will be denoted as rdbms so you may know that uh r d b m s that means relational database management system so that relational database management system is denoting your backend database you are using here we are using sqlite3 how this particular object oriented programming language called ruby is going to be mapped with the rdbms called sqli3 so that is the concept behind this for rm or means object relational mapping so here it is or means object relational mapping so how is going this concept is going to be work so how it is working in this particular ruby on race active record so whenever you are mentioning any object oriented programming concept the important three topics we will be calling as object methods and then class so if you are talking about object that is the basics of ruby and class and methods that is a very basic concepts of ruby programming language whenever you are talking about any backend database table you will be talking about this uh rows columns and table names right rows columns and table names will be there in your rdbms that is nothing but relational database management system so usually your object will be mapped to the rows in your particular backend database okay if you are creating a new row that is mean to be creating a new object in that particular object object-oriented programming language so if you are mentioning the column names that will be mapped with the methods if you are mentioning the tables that will be mapped with the class so this is the idea object are mapped to rows methods are mapped to columns and classes are method class names are mapped to the table names so this kind of concept object is mapped to rows methods are mapped to columns and class name is mapped to table names this mapping is called as object relational mapping this is how activerecord is handling your ruby script with this rdbms that's why this orm framework otherwise this oran concept is very important in terms of active record so very simple o or n okay so this or is nothing but uh it's providing the that is the link between your object oriented programming language you are using and the relational database management system you are using so that is one of the theoretical concept you need to understand next one is what are the basics or uh what are the uh the naming convention this particular rails active record is doing so you can see that kind of topics in this guides for your reference so i just want you to read after the watching the classes as an assignment you can go through this active record basics and you can able to understand the basics and then convention what are the other topics included in this video so here i want to talk about the naming conventions which are followed by this particular active record naming convention means uh you don't need to give any convention for any configurations or setup by default rails will take that automatically the first thing whenever you are creating a model okay with the singular name james will automatically convert into plural okay so if you are giving a article the tab table name in the backend database schema will be automatically generated as articles this is because uh the table is going to have more than one column right so number of columns will be there so automatically rails will pluralize your model name that is the table name article will become articles line items will become line and score item d will become these if its mouse it will become the plural form mice person means people like that this by default naming convention is used by this model or active record it is using this pluralized format okay so that is the first one second one it will add the ids if you are making a table called user the id field which is integer field which will get added to your table by default so you don't need to add any specific id to your table id will be added automatically to rails so if it is easy for you to give the foreign key and primary key relationship over there so that's why rails will use the foreign key primary key relationship by default and it will assign the corresponding table and its id by default you don't need to add the ids okay similar to that whenever you are creating a model it will add from the default timestamp field for example created at updated these two fields are called as time stamps which is used to track uh your records so which way which the record has been created which that has been updated so like that it will automatically store the hovers minute seconds that date with time this field will get automatically added so whenever you are using these kind of uh active record these naming convention and these kind of keema conventions id creations are automatically added by this by the active record so you don't need to spend time on that that will make your code much simpler so we already have one sqlite 3 browser right so on the first installation itself we have used this tool to open our database and check so what i am going to do i am going to open that particular database which we are using already we are using one example project let me open the project in that project we can open the database with this editor called this sqlite db browser for a scaleit3 okay so in this from here you can open your corresponding code and you can able to check your db what are the things i have taught you right now you can check it out so we have this app in this app there is a db folder right here you can find the database so whenever you are using the database you can see the id this field that is the field we uh i just now give you the idea of that so id will be generated automatically and these two fields created and updated will be generated automatically these two fields are called as timestamps okay so right now whenever you are adding the book i mean your whenever you are creating the model name as singular the model name will get pluralized you can see that books okay so if you are creating any other uh table in the name student it will get automatically pluralized at students okay books students okay so like that you can able to see that names are pluralized so these kind of simple simple uh migration naming conventions and then adding the ids and then adding the time stops are followed by rails which will make your configuration much simpler so you can able to add your tables very quickly that's why activerecord is following this kind of simple naming convention and other configurations next one in model using the model what we are going to do so we are going to do the credit operation so we already talked something about the credit operation in the scaffold video so scaffold is doing the basic credit operation see whatever if you are learning mathematics whatever problem you are using either you will be doing addition subtraction multiplication or division in the similar case whenever you are using any web application what is the four operation you will be doing if you are doing anything with the backend database what you will do either you will create a record in the table or you can read it i mean read means get fetch record from the table or you will be updating the table record or you will be deleting the table record so something nothing but the four operations are possible create read update or delete okay so that is called trade operation so those credit operation we already talked about the scaffold uh workflow so the scaffold will do with this credit operation all of this credit operations are based on the model name remember that so model name is nothing but products okay then product dot new product dot say something like that you will be using this particular name of the model model means uh whenever you are referring your code here it is your code open your code with your editor even if you are using that with your editor that would be good so for learning purpose i am using this sublime text too uh you can you prefer some other editors too that is ruby mine or autumn editor or ga edit something like that so i prefer this particular uh is sqli i mean sublime text okay now whenever you are looking at this particular model it will be residing inside the app folder okay so whenever you look at the model you can able to see that corresponding yeah book dot rb student dot dot b these are the two tables we have created already so whenever you are using this books table or student table you will be having this particular books or student so that name of the models will be single i mean this singular and it will be pluralized in the backend database so whenever you are looking at the particular table a class name will be added according to the worm the class name will be mapped to the corresponding table name so you need to use this class name dot new that is nothing but creating an object you already talked about that whenever you want to create a new row you need to use uh that model name dot new that is creating an object similar to that whenever you are adding columns you need to use the methods so in your migration first migration whenever you are seeing that migration file you will be using a method called change it will use a method called a create table to create the records one by one from the table so creation of the columns are mapped to the methods inside your migration migration file and finally the table names whatever the table name you are using here let us say student.rb so this table name is mapped to a class name in rails okay so that's why we are using this forum concept you need to give student dot new that is the way if it's a book table you need to give book dot new okay so this is the code okay book book dot new whenever you are creating a new record you need to give book dot new whenever you are updating book dot update whenever you are saving book dot save whenever you are destroying book dot destroy like that we need to use the method so as we already know about these are the scaffolded code you can see that book name that's the class name used with all of the method book dot all that is nothing but the name of this class okay book remember that so whenever you are using it inside your controller you will be using that with book dot all book dot new book dot save okay so book dot update and then book dot destroy so these kind of methods are used with the model name remember that the model name should start with capital letter because the class name will always start with the capital letter so class that name is student right so it is started with capital letter s so you need to use it with the uh student name yes so you need to make it with capitalized letter first letter should be capital not all the letters first letter should be capital whenever you are using it with the controller or some other places you need to call the class name dot or class name dot new so that is nothing but the book okay so that's why all the cred operations create read update or delete the cred operations will be used with the model name remember that okay so the credit operations are nothing but related to the models which will the basics of create read update and delete operations are taken place based on the model you are creating okay so that's it about this particular active record basics next one we need to talk about one more important topic called validation before that let us talk about uh one more simple concept called migration then we can get into the validation migrations uh we already know whenever you are creating a ruby model i mean rails model the migration files will be created what do you mean by a migration migration is nothing but the meaning of the word moving from one place to another or moving from one to somewhere so here migration means whenever you are adding the db your migrations files will be generated that means whenever you are adding a database the corresponding migration files which will hold the uh structure of the table otherwise schema of the table will be generated you will be adding your migrations or structure for your table over there if you are using any other backend database let me say i am using this backend database of oracle and i am moving into my production environment i am moving it to my test environment like that whenever you are moving from one environment to another environment or one system to another system or one uh database to another database the structure is going to be the same but the database will maybe get changed so in that situation uh your table structure and your schema should be the same right at the time these migrations files will be helpful whenever you are moving your database or your application from one system to another or one environment to another environment these migrations files will be holding the structure of your table so without changing the table structure you can able to recollect all the schema of your table okay so that's why this migration name implies moving from one place to another moving from uh one source to another source in our terms in terms of rails moving from one environment to another environment or one system to another system something like that it is um denoting your on the move okay it's denoting your moving point so that's why the files are called as migration files okay so migration allow you to use some ruby description language that means dsl to change your tables so usually whenever you are creating a migration model file the migrations will be generated automatically so suppose if you are using a command prompt let me say rails generate model model name book okay if you are creating this particular model the corresponding migration file will be generated in your db app migrate folder okay so like this it will get get generated so in this particular book okay this migration file you will be giving the structure of the table after that you will do rate db migrate so these points we already know so this migration file will be generated based on the model you are creating suppose if you want to add one changes to the table for example we are having a table called book right so in this book table there are four fields id name author and then timestamps so i want to add one more field to this table called books okay so whenever you are creating a model that is okay if you want to uh change the structure of this table otherwise if you want to give another columns or if you want to change any column if you want to delete any column i will shrink the table i am talking about the alteration of the table a table is already exist you are going to change the structure of the table or you are going to add a column or you are going to rename a column or you are going to remove a column how you can able to do that if i already have one migration file if i go directly here and adding the columns means that is not correct okay if you are going to the already existing migration file and if you are adding the columns means that is not correct so what you need to do you need to create a migration file alone please listen you need to create a migration file alone okay to uh change these kind of tables so how we can create a migration file alone is is that possible yes it is possible there is a separate command for that using that you don't need to create any more model file you just created only one migration file and you can add the changes to the table which you want to do for example we are having two tables right now here one is let me open one one is book table okay in this table what i am going to do i am going to add a column called i have two columns right now name and then author so i am going to add a column called year okay that is the year of the book and one more thing i am going to uh add or rename your column so in this particular table i have a name called name right name and then author okay so i am going to add a column to the book and i have one more yeah table called student right what i'm going to do to this table let me open the student table here i have name and then age okay so what i'm going to do i'm going to rename this column it's no name right so i'm going to rename it as student underscore name okay and i'm going to delete these timestamps from here i don't need this time stamp for the student table so i want to delete it so like that i want to do some changes to the existing table do two changes i need to do one is i am going to add a column here to the book table one more table is there that is called a student okay to this student table sorry so to this student table i need to rename your call okay remember rename your column ah there is a name i'm going to rename it student underscore similar to that i'm going to delete the timestamp so like i am going to do some kind of changes in my database structure so how i can do these kind of changes from my ah migration file without creating any models so remember whenever you are adding the model migration files will be generated if you want to alter the structure of the table okay i'll drink column or adding you're removing column premium in your column anything you need to create a migration file separately please remember that so whenever you want to create something you need to get into the folder structure we are using this particular path the name is this is the app name grey campus and we are going to use that so right now what you need to do ah create a migration file so we have already generated model right similar to that the same command you can use for creating your migration rails generate migration migration file name you can give any name okay to understand so keep it somewhat in the understandable format not in any short forms or any shortcuts okay so changes to tables so like that i have added changes to table that is my migration file name because whenever you are creating a migration you need to give some name right changes to tables so i have book table and student table i am going to do some changes for that so that's why i have given changes to tables like that you need to give me a meaningful name from the name you can understand what you are going to do with that okay remember that whenever you are creating a migration please don't uh use any shortcuts or any names you just use some names with underscore with all small case letters remember that okay that is one of the best practices of ruby name whenever you are naming a ruby file or anything it is better to use all lowercase letters with underscores to fill give the space between the names so whenever you are creating this rails generate migration migration file name it will add only one migration file remember it won't add model file or anything it will add only one migration file to your database i mean that db folder so using that migration whatever the changes you want to do for example in book table i'm going to add a column in student table i'm going to rename a column and i'm going to delete the timestamps to these changes you can done on your migration file so why are the migration files at a time you can do changes with any number of models not only two tables not only one single table you can alter the tables if you have higher 100 tables also if 100 tables also you can able to 10 or 100 tables you can able to change those tables from your single migration site that is possible okay so this migration this command will generate your migration inside your migrate folder the location is db migrate okay so right now yeah it has been generated db migrate with some 14 digit number i already told you this is nothing but the 14 digit number is nothing but 2020 0 7 27 that is the date time hours minute seconds okay that is nothing but the time stamp this is used to track the migration files which migration we are currently using so now the migration file has been generated right now you just open the file here you can add the changes which you want to add first of all i am going to add this particular add column you may wonder how i'm adding this column okay without referring anything i will tell you where it is and i will show you what are the available methods like add calls okay add column year to books table and you need to mention what is the data type the data type is integer okay so add a column called year to the books table okay add column to the here i mean add the column in the name here uh to the books table with data type integer you may wonder where i can refer this kind of method what are the methods available like add column so those methods are available in this active record migration this guides will show you what kind of change methods you can do ah for example adding a column yeah so adding the column okay two corresponding what are the other methods i will list out yeah here here are the methods okay adding a column adding a foreign key adding an index adding the reference adding timestamps changing the column changing column null create join table create table disable extension drop join extension drop table remove the name index like that a lot of methods are available so we are going to use this add column as a sample i am going to implement three methods right now add column so it will take uh sorry it will goes to books table with here and the corresponding data type integer we are going to add a column to the books table with here the name is here and it will add the data type with integer okay that is the first change next change i want to do is i need to rename the column in the student table okay so instead of add column you add rename underscore column okay and to the students table not books table students table what is the old column name it is available in the name to student underscore so the existing column name is name i'm going to change it with student underscore name that's it so renaming a column will rename some old name to new name okay so in the student table right now the name is us name i'm going to change it up student name that is renaming the account and the third one remove underscore timestamps i am going to remove some timestamps from students so that's it so three methods adding a column in book table renaming a column in students table removing the timestamps from the students table so these are the three changes i am going to do now first let me show you what is the things we are already have books is having id name author created and updated i am going to add a new column called here and students it's like having id name agent created i am going to change this name as a student name and i am going to remove these two time stamps fields okay these are the changes i have requested so remember whenever you have added the migration file don't forget to migrate into your backend database so you need to use rate db migrate so whenever you are using this it will migrate it to the backend database remember that whenever you are adding a model file or whenever you are creating a particular migration file you need to run this command rate db migrate or you can use this particular yeah my grade so now you can get the confirmation migrated so the changes to tables are migrated right now so this is the old format now you just go on refresh okay you will be getting the new format okay so now here has been added here to the books table and the second one student name okay name has been renamed as student underscore name and these two timestamps has been removed created at sub interview okay so if you are getting this migrated message means your success that means there is no uh pending in that migration or you will be getting error messages okay so this error messages shown up means the migrator will be reverted and it will be reverted back to the older version you need to change the syntax in the migration file and you need to come back if there is any error you won't get this message migrated right now you can see a migration has been done with the help of a single migration file okay whenever you want to add anything to a column i mean add a column rename your column remove a column or any kind of changes to the database remember that you need to use this particular this migration files okay not from model file once migrated file cannot be touched again remember that if you are doing uh this migration and you already done with the command rake db migrate here after if you are going to change anything in this file that won't reflect in your database so you don't need to do uh i mean you cannot touch once migrated file again if you want to do any changes create one more migration file and add the changes okay so make sure you are doing all the changes in a single migration file before you are running this command called rate bb migrate once you have run this command the files will get migrated after that you cannot add any changes to the existing migration file if you want to add any more changes you need to create one more migration file and you need to add so that's it about the migration you may wonder what these kind of timestamps which is where it is used so there are three migration files up to now in your mac and database you can see a table will be there in the name schema migrations so that will show up this 14 digit number for tracking purpose so right now we have three migration file the current latest version is this 200 something today's file so like that you can find out the 14 digit numbers are nothing but the timestamp which will be stored in your backend database for tracking purpose that's why each migration file is preceded with this particular 14-digit number okay that is the idea so that's it about the migration files it is very easy what are the available methods we can do it with this particular change method lots of available methods are there you can try them on one by the if you want any reference for them you can able to refer from here okay so i have added three methods for our reference okay so that's it about the migrations very simple to use next one we need to talk about validations okay after migrations uh you can add validations valid what what do you mean by a validation validation is nothing but uh we are making sure uh the data the data you are entering via the model i mean the form whenever you are getting the data from the user maybe we talked about form helpers to design our form right from the form you will be getting the data from the user so for example logging form will be having your email id and password similar to that uh your signup form will be having the email id password captcha and then mobile number bio data everything will be there so whatever the information you are getting via the form will get stored in the backend database so back ends database is used to store your data but we need to make sure what are the data stored in the backend database should be verified one that means you need to store only valid data in the backend database okay so validations are helpful for that why we are going for validation validations are ensure that only valid data is stored in your database so we can we have lots of hackers right now in the weather location field right now uh lots of uh mall functions are happening lots of uh other thread and threads are there that means uh lots of albino and security issues are there in the web application so banking website other important websites are facing lots of issues with the security so that's why we need to make sure whatever the data you are getting from the user should be banned it should be in the valid format it should be in the correct format and it should not be a null record or nil or null empty records we need to make sure only the valid data is get stored in this particular validation so that's why we are using these kind of validations before storing the data into the table okay how we can use this validation see validation should be used with your model file so it cannot be used with their migration it cannot be used with any other techniques it should be used with your model file you have a model file in the app folder right there you need to use that how we can do the validations then so whenever you are adding any wrong fee let us take an example of facebook account or gmail account anything so whenever you are adding that particular any data to any forms okay if you are adding a wrong information it should give you uh that particular error messages okay so let me take one example of that gmail or something so before that i want to show you the forms the gmail forms which are you can find out the error messages okay so i'm going to log to my account okay so when i give wrong password it will tell you this error messages how this error message is coming because of the validations we are using over here okay because of the validation we are using the error messages are coming from your backend database so active record is checking and it is giving your error messages so these error messages uh entering that particular user to be logged in or it is avoiding the unnecessary data to get stored in your backend database that's why we are going for validation so how we can implement these validations in our lives application so as i mentioned you you need to write it in your model file with keyword called validates okay this validates keyword is used to validate your input so you can use different different scenarios or different kinds of validation helpers which are available in your rails application by default so for example ah whenever you are a login or whenever you are creating an account for example let me uh let me create a get into the create forms of this chain [Music] so let me get into the user so whenever you are doing sign up for application okay you can get these kind of forms from google so whenever you are entering your wrong informations or anything it will do lots of kinds of validation for example if you are leaving this empty and then submit it will say enter first name then first name cannot be empty okay if i give the already available username okay then it will say uh this email id is already there okay if i misuse that password i mean if you give wrong password password confirmation then it will give you these kind of things password should we have eight characters so like that in a single form we can do variety of validations so you can do different kinds of validation so for each purpose for example checking your email uniqueness for checking your password confirmation we have different kind of help us with the validation so let us check that before that as i mentioned you you need to write it in your corresponding model file okay with validates keyword and give the columns which need to be validated and the validation helper and you can use that so lots of validation helpers are available we are going to use them one by one okay in our table what we can i mean how we can do the validation let us take our book table okay so in this book table i am going to add some validations for ourselves let us add the checking of the presents i mean we need to check whether any value is present or not so you can check validates the name field is there with presence presence means it will check any data is entered or not so here in this google form if you are not entering any values means it is selling your error message right so similar to that if you want to get the same thing if you are empty entering blank otherwise if you are keeping the field empty it should tell you this you should use this particular field to shade the record like that if you want to get the error message you can use this presence similar to that few form helpers are available let me show you before that okay so acceptance box are accept terms of sense so you may see acceptance of terms of services in some of the pages earlier right now it is not there in some places if you see terms of service update or something will be there i will talk about that so here whenever you have the first helper called acceptance okay you need to give validates terms of service acceptance true in your model when you give that if your particular page is having some pages like this i accept the terms of service if you forget to click on this check box then it will give you error okay so if you want to check whether the check box is being checked or not you can use this uh particular helper called acceptance okay this is one kind of variety of valuation you can do next one is confirmation fee if you're having a google account you have a password field right let me say i have entered this password and i'm entering another password which is the confirmation field but if it's wrong then it will give you the password did not match the email id does not match like that the confirmation field need to be checked right so at the time you can use this confirmation feed okay so that is another helper similar to that exclusion if you want to any particular value to be excluded or it is reserved okay you cannot use that means you can use this exclusion for example if you are using uh some kind of domain names let me say a ww or a us or ca or jp these are referred by keywords which cannot be used with your application at the time you can use the keyword called exclusion so you can give that exclusions inside this particular block of code so that will get extended okay it won't work if you are giving that value it won't allow you to enter the value okay if you want to exclude some value we can use that and then format checking format checking means you can check the format of your input for example here email id is there right i am entering one wrong email id one one one one one something this is not the valid format of your email right so at the time it will tell you carrot so i'm entering correct details but my email format is wrong okay then it will tell you this uh username has to be have a to z character and it should have eight character like that it is telling you the format of the image how it should be there okay so that kind of format checking can be used with this format helper okay you can use it with the some kind of regular expression for it to check your whether your input is good or not okay that is called format checking so for that you can use this helper called formation and the next one is inclusion inclusion means if you want to include any values only those values can be included for example if you are having a size field only three values are possible small medium or large okay only three values are allowed means you can able to use this enclosure okay ah apart from these three size no other third uh otherwise fourth or fifth values cannot be added in that kind of things you can use the inclusion it's just opposite to the exclusion if we are giving only three included values in that kind of scenario you can use this inclusion helper next one is length checking for example the same thing you can take it if i give only four characters for the password it will say please use eight characters so it is counting each and every character and it is checking the length right so that kind of length checking can be done with the help of a helper called length validates length minimum 2 so the minimum length of the character should be 2 like that you can check the length of your existing record next one is numericality if you want to check any integer value for example your date of birth or year of birth should have only the valid stuff let me say and the password is one two three five six seven if you are entering your mobile number or something if you are entering your mobile number or something it should have only this particular let me go to the next page okay if you are trying to add a mobile number or any other mobile number or any other number which should be a numerical value for example it should be integer value you cannot add any string or to that for example phone number right i cannot add this kind of numbers whenever i give that it will tell you the same this mobile number is not good because that should be a numerical value okay this particular phone number should be numerical value not any string or any other value at the time you can use this numericality checking it will check whether the input field is the number field or not so presence we already seen it means check whether the field is there or not if it's not present it will give you message it should be empty it should not be empty you please enter value similar to that we have our own methods as well validates with if you don't want to use any of the available helper and if you want to use on your own you can write with validates with and you can call your own validation methods too okay that is also possible like that rails is providing variety of validation helpers to check your inputs before storing it in the database you may wonder okay these kind of validation helpers are there how to implement that there are two steps okay first you need to initiate the validation or you need to declare the validations in your model file remember that okay like this you need to use it with validates keyword and which field they need to be validated and what kind of validation helper you are going to use and true if you are giving this one that corresponding name will automatically get validated whenever you are trying to submit the form next one every validation helper will give you some kind of error messages default error messages will be given up for example presents so if you are using this presence if the field is not there active record by default it will give you a message called um cannot be empty okay your field cannot be empty so this is the one it's not empty so this error message will be given by active record by default what is your next step the active record is checking for the corresponding field name if it's not present it will automatically give you the error message you need to display that in your form so we have already added the scaffolded form right so in your books this book is a scaffolded one and your form is already there so if you look at the line number 2 to 12 you can see if there is any errors in the book it will tell you this error messages one by one books dot error messages it will be taken with each loop because more than one errors can be there at the time so it will be taking up the message one by one and it will be displayed this code as it is a scaffold code rails has already added the error block automatically remember that if you are using on your manually you need to add this errors lock this is the error block code which is used to display the error messages which are coming while you are trying to submit the form okay so two steps you need to do first initiate the validation in your validation uh i mean in your model file and display the corresponding error messages in your corresponding form wherever you are getting the input from the user so in the corresponding form you need to display the error block okay as it is a scaffolded code the error block code is already there so you need to declare the validation you need to display the error messages in the corresponding form so right now i have added only one validation let me add a few more validations okay let me check the length of the for example length okay length of your name so validates name length minimum 2 so for example this one okay validates name the minimum characters of books name is do okay similar to that i am checking the one more thing let me check yeah this one is uniqueness so if you want to check the uniqueness of your existing email you can check it out if you want to have only the book name to be unique unique means the name should not get repeated okay so the name of the book should not get repeated then you can use the uniqueness method for email it cannot be repeated right for uh email id account number there should be a uniqueness for each user there can be only one email id similar to that if you want to check these kind of uniqueness you can able to use this uniqueness fee so similar to that you can check arthur field also should be there okay or the fin should be there means you can check um the presence for author field not only one field you can add multiple fields as well during the same validation itself you can add multiple fields okay name or there's something like that again so as of now i have added four error messages i mean four validations so let us run this app and check how this validation is being work so i have added present should be there for book name present should be there for author name uh the length of the book name should be minimum two character the book name cannot be repeated so these are the four different kinds of validations i i want to check it right now so let us check this one so before that run the app so whenever you want to run your application don't forget to open your local host colon 3000 that means start your rail server then add this particular corresponding book state so now i am going to get into the books page now i am going to add a new book for us so what is the book name we can give let me say i didn't give any book name and i am clicking on create books sorry so let me click on create book and you will be getting three errand messages what are the error messages are coming up three errors provided this book from being saved name cannot be blank okay name is too short author cannot be blank why the name cannot be blank is coming because this name presents true is checking over the validation what is name is too short minimum true character because we are checking the length of the character with minimum two character why the author cannot be blank because the author name should check the presence by its own so suppose if you are entering all the details let me say book one after one so this book one is already exist okay so it will tell you the name has been already taken because we are checking the uniqueness over here okay so can you forget why this is coming this is coming because this particular error messages are being triggered by your active record whenever you are writing these kind of validations in the corresponding book table it won't allow you to store a record until you are giving the proper value let me give some other value google okay when i create the book now it will get stored book 11 so rename the book and store it will store successfully otherwise it won't allow you to store any records to the book table okay so that's why we are doing these kind of validations to make sure these kind of validations are allowing only the corresponding valid data to be entered into your database okay so very simple case okay you can simply use with two steps declare the database i mean declare the validations and show it in your show the error block messages in your corresponding form file so as it is a scaffolded code it is already there okay so that's it about the validation validation error messages will be come by default whenever you are adding so you don't need to add any of your error messages if you want to add also you can customize your error messages as well that is also very simple the error messages will be coming up by default and it will be displayed worthwhile so like this you can do the validations okay very simple topic that's it about the active record validation what about the remaining topics in this range because these are the two important uh model topics you need to cover what are the other topics so the next one we need to talk about uh this corresponding other validations and other topics that is nothing but association and callbacks so we are talking about the models right so you that is that uh sometimes we can give the relationship between the tables that means you can give one to one relationship or one-to-many relationship or many-to-many relationship so mapping those kind of relationship between the tables can be used with the concept called association that is also one of the concepts in raise models another one is call backs we have written the validations in our in our model file right similar to these you can add some extra options from your model apart from the thread you can add some extra operations from your model those kind of extra triggering methods are called as callbacks that is also one of the important topic in ruby price models and query interfacing so we are using book dot new book dot say book dot update a book dot find some kind of inbuilt methods for the thread operations right so similar to that instead of the raw sql queries you can use inbuilt sql methods which comes with rails by default those method will save your time instead of writing your query so those topics are called as query interfacing so whenever you are using methods instead of the xql queries those are called as query interfacing so these are also associated topic of this rails models okay so relationship and then callbacks and then query interfacing you can extend your knowledge by reading that guides dot ruby on rails dot org this is the official website right you can get those kind of examples and then those kind of references from over here okay but the important topics are migration and validation so we have seen how to do the validations how to do the migrations in this particular video so after watching this video as an assignment you can take the validations and callbacks in your app and try these i mean validations and migrations in your app try the same okay next let us recall what we have learnt so in this video we talked about the active record basic concepts like forum naming convention and something and we have talked about migrations and we have talked about validations too after that associations call that query interest phases are other topics in models as well so thank you so much for watching this video don't forget to like and subscribe to our channel so subscribe to our channel watch the video share it with your friends and make use of all the available resources so stay skilled and stay ahead
Info
Channel: OdinSchool
Views: 3,808
Rating: undefined out of 5
Keywords: ruby on rails tutorial, learn ruby on rails, ruby, ruby on rails tutorials, rails, rails tutorial, web development, programming, how to install ruby on rails, mvp, website, startup, coding, web app tutorial, rails 6, tutorial, ruby on rails tutorial 2020, web app, learn to code, ruby on rails tutorial for beginners, ruby on rails
Id: md7bnEcRAiA
Channel Id: undefined
Length: 56min 17sec (3377 seconds)
Published: Thu Aug 27 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.