Hello! I have 2 questions. 1. controller: @websites = Website.find(:all) view: <% for website in @websites %> <div id="portfolio_item"> <h4><%=website.name%></h4> <%for photo in website.website_photos%> <%= image_tag photo.public_filename, :size => photo.image_size %> <%end%> </div> <%end%> So, i have websites, and they have many website_photos. This code works fine but how can i display for example 2 first photos, not loop through all of them, without putting the logic in the view. I could only achieve this by putting some variables in the view. 2. My second question is about general way how to sort stuff. My first idea was to sort by id of the photo, or maybe make a special boolean field in the table and if it''s true the photo will be listed on the main page, otherwise not. -- Posted via http://www.ruby-forum.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 Feb 9, 5:40 am, Pablo <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hello! > > I have 2 questions. > > 1. > > controller: > > @websites = Website.find(:all) > > view: > > <% for website in @websites %> > <div id="portfolio_item"> > > <h4><%=website.name%></h4> > > <%for photo in website.website_photos%> > <%= image_tag photo.public_filename, :size => photo.image_size %> > <%end%> > > </div> > <%end%> > > So, i have websites, and they have many website_photos. This code works > fine but how can i display for example 2 first photos, not loop through > all of them, without putting the logic in the view. I could only achieve > this by putting some variables in the view. > > 2. My second question is about general way how to sort stuff. My first > idea was to sort by id of the photo, or maybe make a special boolean > field in the table and if it''s true the photo will be listed on the main > page, otherwise not. > > -- > Posted viahttp://www.ruby-forum.com/.you could create a helper, otherwise something like for idx in 0..1 if @website[idx] etc Not sure what the other question you are asking is. You can sort: list.sort {|x,y| x.fld <=> y.fld} --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
You can use .find on the collection. website.website_photos.find :all, :limit => 2 On 2/9/2007, "surfivor" <surfver-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote:> > > >On Feb 9, 5:40 am, Pablo <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >> Hello! >> >> I have 2 questions. >> >> 1. >> >> controller: >> >> @websites = Website.find(:all) >> >> view: >> >> <% for website in @websites %> >> <div id="portfolio_item"> >> >> <h4><%=website.name%></h4> >> >> <%for photo in website.website_photos%> >> <%= image_tag photo.public_filename, :size => photo.image_size %> >> <%end%> >> >> </div> >> <%end%> >> >> So, i have websites, and they have many website_photos. This code works >> fine but how can i display for example 2 first photos, not loop through >> all of them, without putting the logic in the view. I could only achieve >> this by putting some variables in the view. >> >> 2. My second question is about general way how to sort stuff. My first >> idea was to sort by id of the photo, or maybe make a special boolean >> field in the table and if it''s true the photo will be listed on the main >> page, otherwise not. >> >> -- >> Posted viahttp://www.ruby-forum.com/. > >you could create a helper, otherwise something like > >for idx in 0..1 > if @website[idx] > etc > >Not sure what the other question you are asking is. You can sort: >list.sort {|x,y| x.fld <=> y.fld} > > >>--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---