Wasn''t sure what to call the subject, but here''s my issue. I''ve got two models, and for arguments sake they''re called model1 and model2. model1 has many models2 and therefore I use the following within the ''model'' files model1 has_many :models2 model2 belongs_to :model1 .......etc However on my HTML listing page I want to show all the model1''s and their model2 associates. For example, model1 (foo1) model2 (bar1) model2 (bar2) model2 (bar3) model1 (foo2) model2 (bar4) model2 (bar5) model2 (bar6) model2 (bar7) So far I''ve only ever displayed one model and then used association to get it''s children, for example @model1 = Model1.find(params[:id]) @model2 = @model1.model2.find(:all) The above works nicely, however for my new project I do; @model1 = Model1.find(:all) @model2 = Model2.find(:all) I loop over @model1 and then have to loop on @model2 to see whether any of them belong to the @model1 I''m currently reading. Is their a nicer way to ''filter'' @model2 into a new variable containing only the items relating to the current @model1? I just want my code to be cleaner and more ''rubyfied'' Thanks in advance --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Dec 28, 8:40 am, hiddenhippo <reda...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > However on my HTML listing page I want to show all the model1''s and > their model2 associates. For example, > > model1 (foo1) > model2 (bar1) > model2 (bar2) > model2 (bar3) > model1 (foo2) > model2 (bar4) > model2 (bar5) > model2 (bar6) > model2 (bar7)Is @model1.model2s what you''re looking for? ///ark --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
I don''t believe so because model1 is an array of database values, and the relationship to model2 is a one to many. At the moment I i''ve got @model1s = Model1.find(:all) @model2s = Model2.find(:all) When I render out each of the entries in @model1s I have to loop over @model2s in order to find the related values and render those. Normally if I''d only got one value, i.e. not an array, within @model1 then I would do @model1 = Model1.find(1) @model2 = @model1.model2.find(:all) To bottom line it I was just hoping these was a nice rubyish/rails way of handling this. Thanks for you reply and hopefully the above makes things a little clearer. On Dec 28, 9:34 pm, Mark Wilden <m...-OCn100epQuBBDgjK7y7TUQ@public.gmane.org> wrote:> On Dec 28, 8:40 am, hiddenhippo <reda...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > However on my HTML listing page I want to show all the model1''s and > > their model2 associates. For example, > > > model1 (foo1) > > model2 (bar1) > > model2 (bar2) > > model2 (bar3) > > model1 (foo2) > > model2 (bar4) > > model2 (bar5) > > model2 (bar6) > > model2 (bar7) > > Is @model1.model2s what you''re looking for? > > ///ark--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Dec 28, 2007 11:40 AM, hiddenhippo <redaudi-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Wasn''t sure what to call the subject, but here''s my issue. > > I''ve got two models, and for arguments sake they''re called model1 and > model2. model1 has many models2 and therefore I use the following > within the ''model'' files > > model1 > has_many :models2 > > model2 > belongs_to :model1 .......etc > > However on my HTML listing page I want to show all the model1''s and > their model2 associates. For example, > > model1 (foo1) > model2 (bar1) > model2 (bar2) > model2 (bar3) > model1 (foo2) > model2 (bar4) > model2 (bar5) > model2 (bar6) > model2 (bar7) > > So far I''ve only ever displayed one model and then used association to > get it''s children, for example > > @model1 = Model1.find(params[:id]) > @model2 = @model1.model2.find(:all) > > The above works nicely, however for my new project I do; > @model1 = Model1.find(:all) > @model2 = Model2.find(:all) > > > I loop over @model1 and then have to loop on @model2 to see whether > any of them belong to the @model1 I''m currently reading. Is their a > nicer way to ''filter'' @model2 into a new variable containing only the > items relating to the current @model1? I just want my code to be > cleaner and more ''rubyfied''I''d do this with partials. class Model1Controller def index @models = Model1.find(:all) end end views/model1/index.rhtml (or .html.erb if you are using Rails 2.0) <%= render :partial => ''model1'', :collection => @models %> views/model1/_model1.rhtml <% # whatever you need to render the model1 stuff itself %> <%= render :partial => ''model2'', :collection => model1.model2s %> views/model1/_model2.rhtml <% # whatever you need to render a model2 %> -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/ --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Dec 29, 5:00 am, hiddenhippo <reda...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I don''t believe so because model1 is an array of database valuesActually, in my code @model1 was one of those values. @model1.model2s then gives you all the Model2 records associated with that particular Model1 record.> When I render out each of the entries in @model1s I have to loop over > @model2s in order to find the related values and render those.No need to loop. ///ark --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---