Custom Search Queries With Ransack Scopes - Advanced Search & Sort With Ruby on Rails Ransack Gem

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone my name is dean and welcome back to part three and the finale of the ransack gem series today we're going to be using a scope to create a custom sql query that we use to search for ip addresses in remote machines which sounds a lot more complicated than it will be basically the first sixty percent of the video will just be set up to get us a rails application that has the stuff that we're gonna search through and then we're gonna add like 10 or 20 lines of code to do the custom scope it's actually fairly simple to do that but what it'll allow us to do is to sort ip addresses which of course can be a difficult problem because you might have ip addresses that you know have missing zeros in the padding because it might be like 192.168.001 if you try to cast that to an integer that zero zero one those digits are significant you can't just drop them off and if you're just using uh basic ip addresses of course that padding won't be there and then your your number becomes smaller than it actually is so it'll mess up your sorting so we're going to be using postgres and the inet data type uh which will allow us to do our sorting that way now as far as searching goes there's no way for us to do that through ransack by default but what we can do is use ransac's scopes to allow us to create a custom sql query where we can first cast the ip address to a string and then we can do our search on that string just like we would before so we're going to cover that won't take too long hopefully hopefully you'll get something out of this hopefully it'll be helpful but that's enough for me let's go ahead and let's jump into some code okay so we're going to start with a brand new rails app today this is a very basic app just ran a new rails command and we're going to go from there so we're going to start off by actually generating a new scaffold so i'll say rails g scaffold machine i'll give each machine a name and i'll give each machine an ip address now the name will be of type string by default but the ip address we're actually going to make of type inet this is i think is specific to postgres i'm not sure if other databases have this but this essentially allows you to store your ip address as an ipv4 or an ipv6 and it does come with some special logic associated with it which makes searching and sorting it a little bit easier so we'll run this scaffold and hopefully this will generate looks like it did and then we can run a believa rails rails actually i need to set up my database so we're not going to run anything else so just leave that alone for now and then we'll come over to our config and our database.emo file and for this development one we're actually going to replace this so we'll say this needs an adapter or postgresql it is a database and i'll just call this ipfilter video then we'll give it a user of postgres and this is just what i have mine set to a password of password a host of host a port and by default postgres makes that 5432 we'll give it a timeout of 5000 because why not and then we'll give it a pool of 5. then we have to add this to our gem file so let's go ahead and oops let's open up our gem file if i click on the right thing and then in our gem file just add a few gems we'll say gem pg and we'll say gem ransacked i think ransacked let me check my notes to make sure no it's just ransack and then we'll say gem faker and that's just for some of the seated data that we're going to be using so now we can run a bundle install that should install our gems and now i think we can do a rails db create this should hopefully create our database looks like it did and then we should be able to do a rails db colon migrate command to migrate our database and now we can come into our db seeds.rb file and we can try to seed this so we'll say 50. times do and then end and then we can say machine dot create and we'll give each machine a name and for this we can use the faker gem because that comes with uh some stuff and for this let's actually do i don't know internet.url that works so we're not really using it as a name this is just going to put a random website in there that's hopefully fake hopefully i'm not sending you anywhere strange and then the next one is going to be for the ip address and we can also use faker for this so we'll say faker internet dot ip underscore v4 dot address and if we spell address right we clean up the rest of our code we should then be able to run the rails db colon seed command to seed our database uh lowercase seed my finger slipped should be seeding it now that looks good so let's go ahead and let's open up our console and then let's grab our machine dot first and then if we do this dot ip we should be able to see a uh ipv4 in here and you can see it's in this this format here and a lot of this is going to be handled by postgres as we move forward so the next thing we can do is we can actually come over to our controllers and our machines controller and we'll set up our ransack so in here we just have two lines again so say at q equals machine dot ransac we want to ransack with the q parameter then we can say at machines equals at q dot result and we'll say distinct should be true that takes care of our controller logic the next thing we can do is we can come over and create a search form so we'll say views machines we'll create a new file in here and we'll create a partial call it search form.html.erb make sure it has the leading underscore so we get that uh rails partial goodness and then let's create a ruby tag and we'll say search form 4 at q do f and this is just a helper provided by ransack and then we of course need to say end don't need the equals there uh and then in here what we can do is we can say this needs a f.label for the name contains so this is where we'll say the name of the machine contains something and then we'll say f dot search field or the name cont and this is where the name cont was something we covered in episode one it's just saying you know name contains blah blah blah but we also want to be able to search by ip addresses so for this we can say f.label we'll do the same thing we'll say the ip should well let's say the ip should equal something right that sounds good enough uh but then how do we search this well to search it we can just say f dot search field ip search and we'll give this a f dot submit and we should be good to go but instead of submit uh well we'll just leave it like that for now so let's come over to our index.html.erb page because this is where we have to do some more setup now for the name we can do the same thing we did last time so say sort link and we want at q with colon name we'll say name as the thing it should say and then we'll give it a default oops default order order of asc just full screen this because we're getting a little bit messy here we'll close the parentheses add a space that looks good and then for the ip well we can say sort link at q i p i p oops i p address something like that and that should work just fine now we want to come up here and we do also want to render our partial so say machines slash search form something like that so now let's actually come in here and let's go to our routes.rb let's do a route to the machines index page save that looks good refresh this and move this over and if i actually start the rails server i can hopefully watch everything blow up because it's trying to do this thing called the ip search so if i come into the search form this thing saying ipsearch let me change this to ipcontains undefined local variable ip this is in the sort link so let me come into the index page this needs to have a colon in front of it refresh again should be compiling looks good okay so inside our name we have a whole bunch of random info and then for our pip we have a whole bunch of random info so let me scroll down here i'll create a new machine i will call this testable i'll give it an ip of 192.168.0.1 hit create i'll hit back and now let's try to sort this so if i sort by name you can see the testable appears here if i sort appropriately if i try to sort by ip address this should actually by default okay well i wasn't really expecting that to work so now i'm just going to improvise this a little bit so let's pretend that the ip searching and sorting didn't work and you wanted to add a custom logic for that and you wanted to hear a tick tock sound go off in the background because i'm incredibly unprofessional so assuming that's what you wanted what we could do then is we could come over to our models and our machine.rb and we could full screen this and then here we can add a scope the scope allows us to do some custom search logic so for example oh actually it looks like the sorting's not the thing that should be not working it might be the searching so let me try to do a 192 in the search yeah okay the search doesn't work okay i got you gotcha gotcha i've caught up to where i was in the tutorial so this is good so the way that we can make the searching work if we actually use our brains is we can add a custom scope to the model so if we come in here and we say this needs a scope we'll call this ipsearch and we'll pass this a function that just has a queue and this is just our query so when we call ipsearch we want to query or pass in our input query and we want to do something with this what we can do is we can actually call some sql stuff here or some helpers so for example the where helper oops where helper and we can say where so when we have an ip i want to call some some sql on the ip so maybe i want to say i don't know let's convert this to text because we're searching it so we'll say text parentheses ip and then because we're using postgres we want to say i like instead of like and then we want to do single quotes and inside those single quotes we'll say percent hash brace q brace percent and now right up right around now is usually where someone's going to say something like well you should really be using an rl query or whatever it's called because that's that's the more proper way to do it that is true however this is just a example about ransacked i'm not necessarily going to get into you know creating tables and stuff like that for your queries so if you want to just pass in raw sql and you're not worried about you know sql injections or any of that this will work just fine so how you use this now is when you when you call this method the scope you actually need to give it a scope so let's save this so if i try to run this uh we actually need to come into our form i believe so our search form we want to change this from ipcontains to ip underscore search uh and that's just because that's what we called it in here so here it says ipsearch so we want our search form to be the same so let me bump this up a little bit so now if we refresh this page you'll see undefined method ip search uh blah blah machine base so okay our ip search isn't defined so where is our why is our scope not working well unfortunately we need to actually add this to ransacked or our ransackable scopes the way you do this is by defining a method so it's going to say it's going to be def self dot and we say ransackable scopes you can pass in an auth object if you want to underscore auth underscore object equals nil by default and then what we can say we basically need to return something and we just want to return an array that has ip search in it i believe if we do that now things should start to work so let's take one of these numbers i guess we'll take 81 and there should only be a single 81 in here so search for 81 and there's our 81 as the leading number now what if i wanted to do 81.102 you can see here it starts to work just like you would expect it to i can also grab 102 and we get a couple of these but here's our 81 102. so what if i just grabbed this you can see here we're grabbing this entire ip address just like we would expect now i created a couple extra testables so i can search for testable as well but again just like last time combining these two is going to lead to some weird funky behavior so what i can do is say 192.168.0.2 and i can hit search this actually seems to be working just fine i might be full of beans as one of my coworkers would say but that is basically the gist of how you would do a more advanced scope query like this so all you really have to do that's different from before is inside your search form you just add in your search field you call that custom method you come into your model you give it a scope you name the scope whatever you want you pass in your query and then you do whatever you want to with it for your your sql logic and then you just add in the scope to your ransackable scopes and this really allows you to expand ransacked whatever you need uh in you know just a couple lines of code really most of this episode was just setting up the uh the application that we were going to run the ransacked stuff in the actual ransacked code is you know just a couple lines so that's really helpful i really do think this is good it's just you know when you start to try and combine some of your sql logic you can you know it kind of becomes a pain because you're writing more sql uh than you are just using ransacked helpers and at that point you should just really roll your own search engine not try to build off of some gem like this because that's not really the point of the gem but that's going to do it for me let's go ahead and let's jump to the outro video okay and that's going to do it for this small little ransack series i don't know if you can notice but it's about 90 degrees in here it's been pretty hot recently very shiny uh but hopefully this series helped and this video helped and if it did you know don't forget to subscribe and all the other youtube jazz i think the next thing i'll probably be taking a look at is the pg search which is specific to postgres search and if you're interested in some postgres specific tutorials i can also cover that just let me know in the comments down below if a postgres tutorial series is something you'd be interested in i can cover everything from setting it up with rails to uh how to actually just straight up use postgres which of course you can use with other languages and stuff like that just let me know in the comments down below that's going to do it for me thank you so much for watching i hope you have a wonderful day and i will see you in the next video [Music] you
Info
Channel: Deanin
Views: 506
Rating: undefined out of 5
Keywords: deanin, deanin rails, Ransack Scopes, Advanced Search, Advanced Search And Sort, Advanced Sort, Ruby On Rails, Rails, Ruby On Rails Tutorials, Rails Search, Rails Ransack, Ransack Gem, Rails Ransack Gem, Ruby On Rails Ransack, rails 6, ruby on rails tutorial, ruby on rails 6, deanin tutorials new, rails testing, ruby on rails testing, ruby on rails for beginners, ruby on rails tutorial for beginners, learn ruby on rails, deanin tutorials, rails 6 tutorials, rails tutorial
Id: IexdVzZBOrU
Channel Id: undefined
Length: 17min 44sec (1064 seconds)
Published: Tue Jul 06 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.