ruby on rails nested model relationship and routes
I have these models: Sport,
Country,
League
and Match
Also I do have SportsController,
CountriesController
, LeaguesControlle
r and MatchsController
So the question is:
1: how i can set relation between models ?
2: how i can set routs like this
sport/sport_id(slug)/country_id(slug)/league_id(slug)/match_id
sports/soccer/england/premier-league/52346
Note : all of the countries don't have all sports for example Soccer Sport exist in all countries but volleyball exist in some countries
Chris, it would be really great to see a video on how to set up nested routes properly. I have looked at every tutorial online, and nothing makes sense :D
I would also love to see a tutorial on nested routes AND the friendly_id gem used together. I have been struggling through it and it's driving me nuts.
Mike, I can help you with that. I finally figured it out. You can email me: roe@breakdiving.org I can also post how it is done here.
You can see it in action at breakdiving.io/resources/javascript to see how we are doing just that. Come join us :) It’s free to join.
Hi Monroe, thank you for the kind offer. if you could post here when you have time I am sure it would help others as well. Many thanks! ~Mike
If you need to do nested routes, this is one of the best tutorials I have found. Every rails coder should read this: https://www.digitalocean.com/community/tutorials/how-to-create-nested-resources-for-a-ruby-on-rails-application It doesn't go into detail on how to do double nested routes, but the principles are similar, with a few variations. For example, with three nested resources: dog, allergies, medications, for example, the most deeply nested path will be something like: dogs_allergies_medications_path. It took me a while to wrap my head around it, but eventually it made sense. Another lesson: with routes, you can pass multiple models through as parameters, and in fact, you have to in order to get the routes to work, like [@dogs, medications].
Once set up, using friendly ID is straightforward. The key is to first get the routes working normally. Then add friendly URLs thereafter.
There is a really good blog post from Jamis Buck on nesting resources: http://weblog.jamisbuck.org/2007/2/5/nesting-resources. It basically states that as a rule of thumb: resources should never be nested more than 1 level deep.
You could also check out Rails docs, which is a good starting point for nesting resources: https://guides.rubyonrails.org/routing.html#nested-resources
You're welcome Mike!
As for never nesting more than 1 level deep, that doesn't make sense to me, because in our situation, it makes perfect sense: Resources -> Sections -> Resource Links. And they ALL have to be linked to one another. So we have a bunch of resources (for example, Python), then we have 9 sections for Python, and each of those sections have individual links for that particular part of Python study.
I agree that you shouldn't have infinite nesting, but going 2 or 3 levels in is still manageable, and helps to keep our database very organized.
As for a good starting point, I feel that the ruby on rails guides are sometimes more confusing than other resources (at first). While I haven't checked out Richard's suggested blog post, I do know that absolutely nothing made sense until I found that digital ocean tutorial I shared above. After I read that, everything made sense.
As for Friendly ID, once you get things routed correctly, if you have issues still with FriendlyID, let me know!
Thanks guys.. Got it working tonight. You were right Monroe, once you get the nested routes working the Friendly ID was a snap to implement. Am very happy with what I ended up with. Thanks again!
Example post path: /forums/site-news/july-2020-news/