I have a partial, that generates a list of categories. I''d like to use the same partial for two different scenarios where data will be coming from different models. In one case, the partial is grabbing category data directly from the Category model. In another case it''s grabbing it from an associated UserCategory model. Based on the associations, in one scenario, i''d display the category name like this in the loop: category.name. In the other scenario (where I''m first retrieving category info through the UserCategory model, I''d display it like: category.category.name. What''s the best way to use one partial for both situations? Do I alias category name somehow in the model so it can work for both scenarios? I look forward to your suggestions. Thanks! --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
That''s probably the ''cleanest'' (since non-ugliest isn''t really a word) option. The next big question is how far do you take it? How you actually implement it is where some people tend to like one way over the other. You could simply alias the name to a delegated method and call it done. Or, like in my day job, we actually have ''view state'' objects that take much this kind of complexity out of the controllers and models which makes things a bit simpler and easier to test. They are designed to be loaded up and validated (via Validatable[0] and a Forwardable[1] extension, if needed), which is then passed along to the view such that the view does not need to care what kind of "category" it has or how to get the name from it. Hope that helps. [0] http://validatable.rubyforge.org/ [1] http://blog.jayfields.com/2007/02/ruby-forwardable-addition.html -- James Mitchell On Fri, Nov 21, 2008 at 11:44 AM, ressister <ressister-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I have a partial, that generates a list of categories. I''d like to > use the same partial for two different scenarios where data will be > coming from different models. In one case, the partial is grabbing > category data directly from the Category model. In another case it''s > grabbing it from an associated UserCategory model. > > Based on the associations, in one scenario, i''d display the category > name like this in the loop: category.name. In the other scenario > (where I''m first retrieving category info through the UserCategory > model, I''d display it like: category.category.name. > > What''s the best way to use one partial for both situations? Do I > alias category name somehow in the model so it can work for both > scenarios? > > I look forward to your suggestions. > > Thanks! > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks James, that was very helpful. -Alex On Nov 21, 1:56 pm, "James Mitchell" <jmitc...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> That''s probably the ''cleanest'' (since non-ugliest isn''t really a word) > option. The next big question is how far do you take it? How you > actually implement it is where some people tend to like one way over > the other. > > You could simply alias the name to a delegated method and call it done. > > Or, like in my day job, we actually have ''view state'' objects that > take much this kind of complexity out of the controllers and models > which makes things a bit simpler and easier to test. They are > designed to be loaded up and validated (via Validatable[0] and a > Forwardable[1] extension, if needed), which is then passed along to > the view such that the view does not need to care what kind of > "category" it has or how to get the name from it. > > Hope that helps. > > [0]http://validatable.rubyforge.org/ > [1]http://blog.jayfields.com/2007/02/ruby-forwardable-addition.html > > -- > James Mitchell > > On Fri, Nov 21, 2008 at 11:44 AM, ressister <ressis...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I have a partial, that generates a list of categories. I''d like to > > use the same partial for two different scenarios where data will be > > coming from different models. In one case, the partial is grabbing > > category data directly from the Category model. In another case it''s > > grabbing it from an associated UserCategory model. > > > Based on the associations, in one scenario, i''d display the category > > name like this in the loop: category.name. In the other scenario > > (where I''m first retrieving category info through the UserCategory > > model, I''d display it like: category.category.name. > > > What''s the best way to use one partial for both situations? Do I > > alias category name somehow in the model so it can work for both > > scenarios? > > > I look forward to your suggestions. > > > Thanks!--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---