You’re not receiving notifications from this thread.
ruby on rails nested model relationship and routes
I have these models:
Also I do have
So the question is:
1: how i can set relation between models ?
2: how i can set routs like this
Note : all of the countries don't have all sports for example Soccer Sport exist in all countries but volleyball exist in some countries
For 1. have a look at
many-to-many relationship. With Rails this can be done like follows:
has_many: sports, through: :country_sports
has_many :sports, through: :country_sports
The last model is the one that connects the Sport and Country model and for the bare minimum only need
For 2, you can nest the routes like follows:
resources :sports do
resources :country do
resources :leagues do
This is the gist of it, so feel free if you have specific issues with this.
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: email@example.com I can also post how it is done here.
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/