I am making a site with beer info. I want to have the beer name, brewery and style info for every beer. So they all need to be connected. But, I don''t want two styles, i.e. "Pale Ale", "pale ale", "paleale". So I was thinking of making style it''s own model and people could choose from a list, and then add one if it wasn''t present there. But, if I had separate models for brewery also the URLs would be nested like crazy. And, I want to be able to look up all the beers of a certain style or from a certain brewery from the URL: example.com/ style/1, or example.com/brewery/1, as well as brew/1. I know this is an involved question but I just can''t wrap my mind around it. I would appreciate any advice. Thanks for reading. SHORT VERSION: I want 3 models connected, but I don''t want the nested URL craziness. How? Joel -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Have a look at this article: http://weblog.jamisbuck.org/2007/2/5/nesting-resources Short version: it''s better to limit nesting to one level. Shallow nesting might also be helpful. See section 3.8.4: http://guides.rubyonrails.org/routing.html#restful-routing-the-rails-default -- miles -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Finally - a subject near and dear to my heart - beer! One thing to remember about nested resources is its best to nest one level at a time. Otherwise you get very complicated url''s. Depending on how you set the relations, you could have beer as a nested resource of brewery and then set a separate nested resource relationship between beer and style. map.resources :beers map.resources :breweries map.resources :styles map.resources :breweries, :has_many => [ :beers ] map.resources :beers, :has_many => [ :styles ] So when you are dealing with breweries, you are only dealing with beers. The same thing happens when you are working with beers, except now you have the styles sub-resource. Looking at this problem though it seems like a better solution would be a has_and_belongs_to_many relationship between beers,breweries, and styles. A brewery can make several different beers each of a different style after all. To do this you would create a join table that only includes key fields (beers_id, breweries_id, styles_id). I found a tutorial that should point you in the right direction: http://www.buildingwebapps.com/podcasts/79342-resources-page-links-categories-and-habtm/24793-show-notes On Jul 23, 7:42 pm, Joel Klabo <joelkl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I am making a site with beer info. I want to have the beer name, > brewery and style info for every beer. So they all need to be > connected. But, I don''t want two styles, i.e. "Pale Ale", "pale ale", > "paleale". So I was thinking of making style it''s own model and people > could choose from a list, and then add one if it wasn''t present > there. > > But, if I had separate models for brewery also the URLs would be > nested like crazy. And, I want to be able to look up all the beers of > a certain style or from a certain brewery from the URL: example.com/ > style/1, or example.com/brewery/1, as well as brew/1. > > I know this is an involved question but I just can''t wrap my mind > around it. I would appreciate any advice. Thanks for reading. > > SHORT VERSION: > > I want 3 models connected, but I don''t want the nested URL craziness. > How? > > Joel-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Joel Klabo
2010-Jul-24 15:58 UTC
Re: Trying to pick a model setup without ridiculous routes
Thanks, I''ll read up and come back. Looks like a good solution. On Jul 24, 7:53 am, Agoofin <thebserv...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Finally - a subject near and dear to my heart - beer! > > One thing to remember about nested resources is its best to nest one > level at a time. Otherwise you get very complicated url''s. > > Depending on how you set the relations, you could have beer as a > nested resource of brewery and then set a separate nested resource > relationship between beer and style. > > map.resources :beers > > map.resources :breweries > > map.resources :styles > > map.resources :breweries, :has_many => [ :beers ] > > map.resources :beers, :has_many => [ :styles ] > > So when you are dealing with breweries, you are only dealing with > beers. The same thing happens when you are working with beers, except > now you have the styles sub-resource. > > Looking at this problem though it seems like a better solution would > be a has_and_belongs_to_many relationship between beers,breweries, and > styles. A brewery can make several different beers each of a different > style after all. To do this you would create a join table that only > includes key fields (beers_id, breweries_id, styles_id). > > I found a tutorial that should point you in the right direction:http://www.buildingwebapps.com/podcasts/79342-resources-page-links-ca... > > On Jul 23, 7:42 pm, Joel Klabo <joelkl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > I am making a site with beer info. I want to have the beer name, > > brewery and style info for every beer. So they all need to be > > connected. But, I don''t want two styles, i.e. "Pale Ale", "pale ale", > > "paleale". So I was thinking of making style it''s own model and people > > could choose from a list, and then add one if it wasn''t present > > there. > > > But, if I had separate models for brewery also the URLs would be > > nested like crazy. And, I want to be able to look up all the beers of > > a certain style or from a certain brewery from the URL: example.com/ > > style/1, or example.com/brewery/1, as well as brew/1. > > > I know this is an involved question but I just can''t wrap my mind > > around it. I would appreciate any advice. Thanks for reading. > > > SHORT VERSION: > > > I want 3 models connected, but I don''t want the nested URL craziness. > > How? > > > Joel-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Dave Aronson
2010-Jul-25 01:30 UTC
Re: Trying to pick a model setup without ridiculous routes
On Fri, Jul 23, 2010 at 19:42, Joel Klabo <joelklabo-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> But, if I had separate models for brewery also the URLs would be > nested like crazy.DISCLAIMER: fairly new to Rails myself, but not new to databases, nor to software engineering (and coding concepts such as separation of concerns), so this is sort of written from that POV. The Rails-ish way may be different, but the following is what Makes Sense To Me. You don''t need to have them so closely coupled, nor to have the models nested at all. The brewery, style, etc. could all be parameters of an individual beer. Then you can slap a search on top of your index function. So if you want to know what doppelbocks are made by the Foo Manchoo Brew Krewe, you could do index?style=doppelbock&brewery=Foo%20Manchoo%20Brew%20Krewe (or whatever syntax you decide on for encoding multi-word things). And of course to find all doppelbocks, or all from the FMBK, omit one or the other of the parameters. And of course you could support other parameters, like minrating= some number of stars, possibly separated by rating authority, and so on. -Dave -- Specialization is for insects. | Professional: http://davearonson.com -Robert Anson Heinlein | Programming: http://codosaur.us -------------------------------+ Leadership: http://dare2xl.com Have Pun, Will Babble! -me | Et Cetera: http://davearonson.net -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
jufemaiz: joel courtney
2010-Jul-27 07:55 UTC
Re: Trying to pick a model setup without ridiculous routes
Could do all sorts of even more fun things with that too. map.resources :styles, :has_many => [:beers] map.resources :styles, :has_many => [:breweries] map.resources :breweries, :has_many => [:styles] Depending on how the user wants to go through (and the design of the backend)... On Jul 25, 12:53 am, Agoofin <thebserv...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Finally - a subject near and dear to my heart - beer! > > One thing to remember about nested resources is its best to nest one > level at a time. Otherwise you get very complicated url''s. > > Depending on how you set the relations, you could have beer as a > nested resource of brewery and then set a separate nested resource > relationship between beer and style. > > map.resources :beers > > map.resources :breweries > > map.resources :styles > > map.resources :breweries, :has_many => [ :beers ] > > map.resources :beers, :has_many => [ :styles ] > > So when you are dealing with breweries, you are only dealing with > beers. The same thing happens when you are working with beers, except > now you have the styles sub-resource. > > Looking at this problem though it seems like a better solution would > be a has_and_belongs_to_many relationship between beers,breweries, and > styles. A brewery can make several different beers each of a different > style after all. To do this you would create a join table that only > includes key fields (beers_id, breweries_id, styles_id). > > I found a tutorial that should point you in the right direction:http://www.buildingwebapps.com/podcasts/79342-resources-page-links-ca... > > On Jul 23, 7:42 pm, Joel Klabo <joelkl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > I am making a site with beer info. I want to have the beer name, > > brewery and style info for every beer. So they all need to be > > connected. But, I don''t want two styles, i.e. "Pale Ale", "pale ale", > > "paleale". So I was thinking of making style it''s own model and people > > could choose from a list, and then add one if it wasn''t present > > there. > > > But, if I had separate models for brewery also the URLs would be > > nested like crazy. And, I want to be able to look up all the beers of > > a certain style or from a certain brewery from the URL: example.com/ > > style/1, or example.com/brewery/1, as well as brew/1. > > > I know this is an involved question but I just can''t wrap my mind > > around it. I would appreciate any advice. Thanks for reading. > > > SHORT VERSION: > > > I want 3 models connected, but I don''t want the nested URL craziness. > > How? > > > Joel-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
jufemaiz: joel courtney
2010-Jul-27 07:55 UTC
Re: Trying to pick a model setup without ridiculous routes
Could do all sorts of even more fun things with that too. map.resources :styles, :has_many => [:beers] map.resources :styles, :has_many => [:breweries] map.resources :breweries, :has_many => [:styles] Depending on how the user wants to go through (and the design of the backend)... On Jul 25, 12:53 am, Agoofin <thebserv...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Finally - a subject near and dear to my heart - beer! > > One thing to remember about nested resources is its best to nest one > level at a time. Otherwise you get very complicated url''s. > > Depending on how you set the relations, you could have beer as a > nested resource of brewery and then set a separate nested resource > relationship between beer and style. > > map.resources :beers > > map.resources :breweries > > map.resources :styles > > map.resources :breweries, :has_many => [ :beers ] > > map.resources :beers, :has_many => [ :styles ] > > So when you are dealing with breweries, you are only dealing with > beers. The same thing happens when you are working with beers, except > now you have the styles sub-resource. > > Looking at this problem though it seems like a better solution would > be a has_and_belongs_to_many relationship between beers,breweries, and > styles. A brewery can make several different beers each of a different > style after all. To do this you would create a join table that only > includes key fields (beers_id, breweries_id, styles_id). > > I found a tutorial that should point you in the right direction:http://www.buildingwebapps.com/podcasts/79342-resources-page-links-ca... > > On Jul 23, 7:42 pm, Joel Klabo <joelkl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > I am making a site with beer info. I want to have the beer name, > > brewery and style info for every beer. So they all need to be > > connected. But, I don''t want two styles, i.e. "Pale Ale", "pale ale", > > "paleale". So I was thinking of making style it''s own model and people > > could choose from a list, and then add one if it wasn''t present > > there. > > > But, if I had separate models for brewery also the URLs would be > > nested like crazy. And, I want to be able to look up all the beers of > > a certain style or from a certain brewery from the URL: example.com/ > > style/1, or example.com/brewery/1, as well as brew/1. > > > I know this is an involved question but I just can''t wrap my mind > > around it. I would appreciate any advice. Thanks for reading. > > > SHORT VERSION: > > > I want 3 models connected, but I don''t want the nested URL craziness. > > How? > > > Joel-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
jufemaiz: joel courtney
2010-Jul-27 07:55 UTC
Re: Trying to pick a model setup without ridiculous routes
Could do all sorts of even more fun things with that too. map.resources :styles, :has_many => [:beers] map.resources :styles, :has_many => [:breweries] map.resources :breweries, :has_many => [:styles] Depending on how the user wants to go through (and the design of the backend)... On Jul 25, 12:53 am, Agoofin <thebserv...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Finally - a subject near and dear to my heart - beer! > > One thing to remember about nested resources is its best to nest one > level at a time. Otherwise you get very complicated url''s. > > Depending on how you set the relations, you could have beer as a > nested resource of brewery and then set a separate nested resource > relationship between beer and style. > > map.resources :beers > > map.resources :breweries > > map.resources :styles > > map.resources :breweries, :has_many => [ :beers ] > > map.resources :beers, :has_many => [ :styles ] > > So when you are dealing with breweries, you are only dealing with > beers. The same thing happens when you are working with beers, except > now you have the styles sub-resource. > > Looking at this problem though it seems like a better solution would > be a has_and_belongs_to_many relationship between beers,breweries, and > styles. A brewery can make several different beers each of a different > style after all. To do this you would create a join table that only > includes key fields (beers_id, breweries_id, styles_id). > > I found a tutorial that should point you in the right direction:http://www.buildingwebapps.com/podcasts/79342-resources-page-links-ca... > > On Jul 23, 7:42 pm, Joel Klabo <joelkl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > I am making a site with beer info. I want to have the beer name, > > brewery and style info for every beer. So they all need to be > > connected. But, I don''t want two styles, i.e. "Pale Ale", "pale ale", > > "paleale". So I was thinking of making style it''s own model and people > > could choose from a list, and then add one if it wasn''t present > > there. > > > But, if I had separate models for brewery also the URLs would be > > nested like crazy. And, I want to be able to look up all the beers of > > a certain style or from a certain brewery from the URL: example.com/ > > style/1, or example.com/brewery/1, as well as brew/1. > > > I know this is an involved question but I just can''t wrap my mind > > around it. I would appreciate any advice. Thanks for reading. > > > SHORT VERSION: > > > I want 3 models connected, but I don''t want the nested URL craziness. > > How? > > > Joel-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.