#229 AVO Directory. Associations, Search

18/02/2025
avo
  • 0:00 About Directories
  • 0:58 [Create app](https://github.com/corsego/avo-directory/commit/5f82341b5fe41241a52291ee7bf4b41c30f5ccc0)
  • 1:32 [Install AVO](https://github.com/corsego/avo-directory/commit/97af2a53685ce336587527a6f81ac384bee19ee6)
  • 2:42 [Listing model](https://github.com/corsego/avo-directory/commit/de32df79a064ecfb83bd854e215ac5239dfc39d2)
  • 4:20 [Category model](https://github.com/corsego/avo-directory/commit/843e274f41c0462483db25fb35c7c523b55c6216)
  • 5:30 [CategoryListing model](https://github.com/corsego/avo-directory/commit/24cfd386af0e1e356fbe3be8651bfc9f4559a2d5)
  • 6:13 [Category - Listing associations](https://github.com/corsego/avo-directory/commit/e24dc738ae8f54c3ce173f5925d746358cd80656)
  • 9:56 [Select only unassociated records](https://github.com/corsego/avo-directory/commit/df3f04642c73f51bd98b1eee17d8c1d46f44272c)
  • 12:21 [Search categories and listings](https://github.com/corsego/avo-directory/commit/1268f0e77d06a9d09d2be2d557b8196a93301452)
Transcript
Hello friends. So recently I was talking to Zach Gilberg that, uh, created Rails Market that is a director of Ribbon Rails, uh, uh, tools. And a directory is basically where you have, uh, different listings that you can search for. And they are, uh, all stored in different categories. And I was thinking maybe we can build a public directory using avo. So, uh, if, uh, usually you would use AVO for like, something internal, like, uh, an internal CRM or an admin backend. Maybe we can use AVO as an external tool that, uh, uses that are not logged in, could use. And actually, here's an example of a tool that uses avo, uh, and it doesn't require log in. So, uh, here I'm like, on the homepage, I can view different, uh, resources, sold, filter, search, uh, visit any of these resources and see details. So maybe we can use AVO for building a public directory product. Now let's start with creating a new rails application, rails new, uh, Avvo directory app, uh, cd, uh, Avvo directory app, uh, rails, db create, db, migrate, uh, gi add all. Let's create the, let's commit our creation of the app. Okay, so we've created the app. Let's open in the code, uh, editor, and let's start the rail server. So yeah, we have a basic, uh, new app that we have created. Let's install, uh, apple. So I'm going to run this, uh, script, um, install in AVO again. Uh, we will, uh, run the server to have a look if we have it, uh, actually installed. I will, uh, have a look at my roots. So in route I have the Mount Avo engine that goes to a configuration route path. If we go to VO initialize it, they have just generated the root part is slash vo. So to access vo, which go to root path slash vo. Here it is, we have VO installed. Uh, okay, let's, uh, actually make the route of our application vo. So you just go here and you have vo, we'll just make it by heaven slash here. And you are going to local host 3000, and you see vo. So it is the homepage of, uh, all our application. Let's save our changes. Get add all, get, commit, message, install vo. Okay. Now, uh, if I will a directory, we will want to have, uh, listings and the listings can be inside Greece. And a listing, uh, most importantly has, uh, A URL. It also can have a title, a description, some kind of tags. Uh, but let's start with a listing that just has a URL. So I will create a new model. I don't need to create a scaffold because everything is going to be inside avo, just a model rail generate model listing. And a listing will have A-A-U-R-L or website name. Let's make it URL. That'll be text because it can be longer than just a regular stream. Oh, yeah, that'll be stream, whatever, okay. Rails to be migrate. And, uh, you see, uh, when we created this model, uh, as we already have Ava installed it, created the a resources for these listings. So if we go to avo, let's open it. We already have the resource for listing here. We can add different listings. Let's add, for example, super ails. So we have super ails. If we can add the drifting ruby. And, uh, this would be possibly in the same category. The category would be, or rail screen costs. Uh, let's first of all, save our changes. So gi at all, gi com message, uh, listing model. Okay? So we have created the listings and listings should belong to, uh, categories. And the same listing can potentially be in multiple categories. So let's create a category, model, rail, generate model, category. Category can have a name and maybe a description. Description will be text. And, uh, okay, uh, rails did be my, so now we have, uh, listings and, uh, categories, but they are not associated, uh, between each other. Let's create category as, uh, ruby on rails, screen costs. Okay? Uh, so to associate, we need, uh, maybe a joint table, uh, because, uh, listing can be in multiple categories. So, uh, the, it'll name it, uh, uh, category listing. Now, let's say our changes from, uh, creating the category model, get at all, get, comment, message, uh, category model. Okay? Now let's create the gen table for, uh, category listing. Uh, real generate model category, listing, category references and listing references. Okay, uh, rails. DP migrate, uh, bin dev. Here we have this, uh, category listing. And, uh, this is how we would associate categories and listings. Let's save our changes again, get the status, get add all it. Commit message, uh, category listing model. I like making such smaller atomic prs to, uh, have like steps, steps, steps, uh, of my work more visible. Okay, so now let's do the associations. So, uh, let's say listing, uh, has many, many category listings, uh, dependent, destroy, and has many categories through category listings. And the category listing already has the associations and category will have something similar. It'll just have, has many listings. Okay? These are the basic associations. Let's also add some validations like validates, uh, uh, URL presence. True. Your listen has a URL, right? Yes. And the category of the validate presence of a name validates name presence, true. Uh, let's, uh, yeah, we can validate uniqueness. We don't have to. Uh, but yeah, for listing, let's also actually validate uniqueness so that they don't have duplicates. Uh, okay, we have listings and categories and, uh, they are associated. Uh, now let's go back to our application and see if we can associate them in the other interface. So let's open the, uh, category and, uh, try associating listings. So we would want to add the association between the categories and listings inside a, I would go to app av, uh, category. And here it'll be field, uh, listings, uh, as has many. Okay. And, uh, let's try a attach a listing so we can select a listing. You see, we have just the ideas at the moment. So, uh, we don't want the ideas. We want to have, uh, the list in the, uh, URL visible. So inside the listing, we'll say something like, sell of the title will be URLI will refresh. And here we already have the URL. So it is, uh, now a bit more readable. I will attach this one, attach, uh, we can attach another, but you see, uh, I have already attached this listing and I can still try attaching it again. Let's try again and see, have the same listing attached to device. So, uh, this is not good. I will add some validation for not being able to attach the same listing twice to category in category listing model. So here I will have, uh, uh, validates category scope listing and validates, uh, uh, listing ID scope category, just to be double. Sure. Let me now, uh, remove this attachment. I will try attach the super rails once again. And you see I get the error. So I cannot attach the same listing to category device. So it works. I can kind of attach, uh, uh, listings to category. And if I open a category, I see the listings that are attached. I open a listing. I don't see the categories that the listing appears in. So inside the, uh, inside VO listing, I will say field, uh, categories, as has many. Okay, we have the categories. I can try attach category. And, uh, yeah, I see this category I try to attach. And you see we get an error because it is already attached, so looks good. Uh, but now let's make it so that we don't even have, uh, the possibility to select, uh, uh, something that is already attached for it not to be present in the down. So, uh, to see only the ones that are not attached, we'll have to do something a bit deeper. So inside the category has, uh, listings, as has many we'll say through category listings. And we'll also say, attach, uh, scope, uh, lambda. And we will say, query there. Uhnot. Uh, ID parent, uh, category listings, select, uh, listing id. Okay, so now in theory, I refresh attached listing. Yes, I see all the listing that are not attached yet. Okay, uh, now I will try again. Refreshing. You see, I have no options for Slack because, uh, uh, all the listing attached to this category. Let's do the same on the category side. Oh, on the listing side. So only see categories that are not added yet. I will, um, go to this, go to the listing, and here has many categories, as has many. Um, yes, uh, select category id. Okay, refreshing. Okay. Yes, looks good. So now we can, uh, attach only, uh, categories that are not attached yet. Okay, let's save our changes. So, uh, get at all. Get, commit, message, uh, attach, uh, only not yet associated records or better, uh, select and associated records only. Okay. Uh, next, let's try adding the search. So at the moment we just have, uh, listings categories, and we won't be able to, uh, find a listing or find a category. So, uh, go into the other docs. Let's go to the docs. Let's open search. Um, enable search for results. And here is an example of adding search. So let's add search to our, uh, listing model. Yeah, we actually even have it here commented out by default. So we are going to search, not by the id, but the, uh, URL. So URL, uh, contains, uh, let's, uh, yeah, add gem ransack. And you see it is also a free feature. Uh, so I'm adding the gem ransack to our gem file bundle. Okay, let's, uh, see if search by U lves going to listings. Um, you see we have this run needs, uh, runable attributes. So inside the model of listing, they are going to say, uh, the self sackable attributes, URL. And, uh, well, we don't need forensic associations at the moment, but we might want to add categories later. Let's leave this blank. Okay, let's try searching. So super rails. And you see if we find super rails, I click on it and it opens this specific crystal source opens. I have categories set here, device. So going here to the listing, I'm going to remove one of these associated categories. Yeah, okay, now categories are here. Just funds. Looks good. Uh, now, uh, yes, search for listings works. Let's add search for categories. Uh, it's going to be similar. So category rb, uh, cell sackable attributes. Uh, yeah, I'll do it in the same synex as here. Uh, will be name and, uh, I think it's description. Yes, it is description. And, uh, in category resource, we are going to enable search. So self search, we'll say, name contains, uh, or description contains. Okay, let's, uh, refresh. Now you see I've, uh, commented this, uh, out. So now we have search. I will try searching. Uh, ruby works. Let's try searching by, uh, uh, a description. So let's say, uh, screenshot tools will be the name of the category. Take a picture, uh, of a URL. Now going into categories, I'm going to search, uh, picture. And you see it, uh, finds the screenshot tools. So it, uh, works. Search books for both categories and listings. Let's save our changes. Uh, get status, get at all, get comment, message, um, search categories and listings. Okay, looks good.
0
Join the conversation
Sign in to leave comments and save lessons to your watchlist.
We collect your email address, name and username to create your account. We do not share your email address with anyone else.