All threads / ruby on rails nested model relationship and routes

Ask A Question

Notifications

You’re not receiving notifications from this thread.

ruby on rails nested model relationship and routes

Qasem Hajizadeh asked in Rails

I have these models: Sport, Country, League and Match
Also I do have SportsController, CountriesController, LeaguesController 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

For 1. have a look at many-to-many relationship. With Rails this can be done like follows:
Sport.rb
has_many: :country_sports
has_many: sports, through: :country_sports

Country.rb
has_many :country_sports
has_many :sports, through: :country_sports

CountrySport.rb
belongs_to :sport
belongs_to :country

The last model is the one that connects the Sport and Country model and for the bare minimum only need country_id and sport_id.

For 2, you can nest the routes like follows:
resources :sports do
resources :country do
resources :leagues do
resources :matches
end
end
end

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 protected] 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/

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 35,699+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.