Ruby on Rails - Has Many Associations

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
how is everyone doing today in this video we are going to be going over something very essential in Ruby on Rails and that is associations model associations so whether your objects can have references to many other objects or just one object for instance let's say you have an account which we're going to call it user does that user possibly have many characters they can play in the game or do they have many tweets they can use if they're you if you're making a social network okay so what I did here is I simply made a simple rails project called active record associations and we're gonna CD into that and now what we're gonna do is we're going to make a simple model and I'm gonna make a model called rails G model and we're just let's call it user for now I'm gonna give it a name I'm give an age I will make that page an integer and we'll give it a bio which is text we will actually use any of these categories but I just want to explore our options text you would use if you needed more space than a standard string okay so the next thing we're gonna do is I'm also gonna make a tweet model and a user is gonna have many tweets so I'm going to do rails G model I'm going to make it tweet and the tweet is going to have a name and some content and we're gonna make that text so we're gonna do that too and we're gonna make the migration files for both of those okay so now I'm gonna Bhim into both of those files and I have not one migrate yet and the reason is because we are going to set up I'm gonna set up a belong I'm gonna set up a has many relationship between user and tweet the has many relationship is the most common relationship in active record and Ruby on Rails in my opinion so the way we can do this is so we see we have a category for name and text well we also need a category so tweet knows about user so tweets need a column references the users and the way we can do this is we're going to is every object is gonna have an ID so every tweet as an ID starting from one going up depending on the order you create them and every user has it ID starting from one and going up so we're going to I'm simply going to make a column called belongs to user so the way we're telling our tables we're saying hey you were going to have a table called tweets and that table is going to have a column for a name some content and it's gonna have a user ID column so that you know this tweet this particular one tweet now belongs to a user and so also we're gonna say the index option for true now an index allows us to basically sort through objects faster so it allows us to find tweets easier and I'll do another video where I actually prove that but you don't need this option if you don't want so I set the index true and I set a tea dot belongs to method I'm going T dot belongs to method now we're gonna run right DB migrate wit that belongs to and hopefully we don't get anything else now we'll new one DB migrate our DB / schema file gets updated and if we take a look at it it used to be blank before I ran this we now see we've a crate table called tweets it's always the plural version of your model so tweets and users and we can see here we have everything we said we said we have name content but when I did that user ID column when I said T dot belongs to user this is what it created it created this column called T dot integer user ID so it's referencing the ID of the user and this index got created that says tweets it's a tad indexed tweets that references the user ID so now every user every tweet can reference the user ID well not exactly yeah we're not fully done yet we also need to make this code accessible to act we've made space in our database for it but we haven't taught Ruby about this relationship or talked Ruby on Rails about this relationship and the way you can do that is we can access our model file and we can access our model file user first and we need to test say that our user has many tweets so that hey users have many tweets in our user file and we also need to go to our tweet model file and say that it belongs to user so notice how I use tweets here because we can have as many tweets if we did a has one relationship which we'll cover in another video we would just use has one tweet singular but we have many tweets so it makes sense always think of it in that case just it should make sense you can't has many tweet that would not make sense and so we also belong to a user so seems pretty good another thing we can do is we can also make it that every time we destroy a user that all its tweets get deleted that our associate associated with it so Whitney has many I can do an option that is dependent destroy and what this does is it make sure that any time a user is destroyed all its tweets are destroyed as well okay pretty pretty easy boom we're done with our coding for today so now I'm gonna go into my rails console which I can do with Whale C or I can type it out myself and we have this up so now let's create a user so when do user 1 equals user dot create and let's see I didn't make anything for this user so I'm just gonna do user dot create and let's do user 1 dot tweet tweets so now if we want to access all of our tweets we none so what we can do is we can do user dot tweet tweets we can now create a tweet so we'll do that tweet one equals tweet dot 3/8 and mix we'll do some content hi there and we'll make one and so now we have a tweet so now we'll do user 1 dot update tweets and we'll give it a tweet tweet 1 and we will update it ok so now we say we I updated the database by saying user 1 dot update tweets and we gave it an array of 1 tweet so now if I do user 1 dot tweets hopefully we now have we get an array we get an active record array back which has our one tweet in it so let me do user 1 dot tweets count and we now have user 1 now has one tweet ok so now let's let's try it this way well let's not do that yet we don't want to add it so we got an we got a database we got our active record Association back which shows okay hey we have tweets they can be associated with one another and everything like that and so now let's look at our tweets here by looking at user 1 dot first we can see here that our tweet ID is 1 its name is nil its content is high there but it has a user ID of 1 and if I look at user user 1 ID it's ID is 1 because I've only created one tweet and one user so this tweet has the user ID 1 and so now if I create another tweet tweet dot to create and let's give it a name and I'll get a name second tweet and that's all I do that is all I will do something I could do is I could set the user to be user 1 so now oh I got an error and that's because it did not expect that input and that is because I have I had to ending braces right there so now I prayed my second tweet but I said the user was user one so now if I do user 1 dot tweets dot count I now get two and now have two tweets because I created my second tweet and I associated with my user one object so it so what happened is when I set it to user it when I set user : user one it went in got the user one ID which is it got the user one ID and then set that object so now I can look at it and it says select count from tweets where user ID equals one so now we can see here the tweet ID is - it's the second tweet I created but the user ID is still one so I have two tweets but they're both referencing user one so then when I do count I get two so user one has two tweets it could have a thousand tweets you could have two users and each of them could have different tweets now a tweet can only belong to one user in our case remember a user can have many tweets but a tweet can only have one user now there is a way to have for instance a tweet could belong to multiple users but that would not but that this problem does not this solution does not solve that problem in this case we're assuming that hey your user has his tweets and every user has their own tweets and they can't intermingle with each other there are cases where you would want that to happen guys I hope you enjoyed this video trying to make them simpler let me know if you like it please give a like please subscribe if you want and let me know what I can do better I'll see you guys later
Info
Channel: Apple Juice Teaching
Views: 13,217
Rating: undefined out of 5
Keywords: ruby, rails, con, learn, tutorial, linux, mac, steve, jobs, college, associations, model, view, controller, google, stackoverflow, video, study, help, first, time
Id: DAjfsW4gqGo
Channel Id: undefined
Length: 10min 44sec (644 seconds)
Published: Sat Jul 23 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.