I want to write this in rails: @aux= @rep_tickets.rp_parts.size for i=0, i< @aux, i++ @rep.ticket.price=@rep.ticket.price + @rep_tickets.rp_part[i].price end -- 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 -~----------~----~----~----~------~----~------~--~---
John Smith wrote:> I want to write this in rails: > > @aux= @rep_tickets.rp_parts.size > > for i=0, i< @aux, i++ > @rep.ticket.price=@rep.ticket.price + @rep_tickets.rp_part[i].price > end >@rep.ticket.price = @rep_tickets..rp_parts.map(:&price).sum --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Russell McConnachie wrote:> John Smith wrote: >> I want to write this in rails: >> >> @aux= @rep_tickets.rp_parts.size >> >> for i=0, i< @aux, i++ >> @rep.ticket.price=@rep.ticket.price + @rep_tickets.rp_part[i].price >> end >> > > @rep.ticket.price = @rep_tickets..rp_parts.map(:&price).sum >Wow, fat fingered that. @rep.ticket.price = @rep_tickets.rp_parts.map(&:price).sum --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Russell McConnachie wrote:> Russell McConnachie wrote: >> @rep.ticket.price = @rep_tickets..rp_parts.map(:&price).sum >> > Wow, fat fingered that. > > @rep.ticket.price = @rep_tickets.rp_parts.map(&:price).sumDoesn''t seem to work:>> @repair_ticket.total_price = @repair_ticket.rp_parts.map(:&price).sumSyntaxError: compile error (irb):71: syntax error, unexpected tIDENTIFIER, expecting '')'' @repair_ticket.total_price = @repair_ticket.rp_parts.map(:&price).sum -- 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 10 Jan 2008, at 18:23, Russell McConnachie wrote:> > John Smith wrote: >> I want to write this in rails: >> >> @aux= @rep_tickets.rp_parts.size >> >> for i=0, i< @aux, i++ >> @rep.ticket.price=@rep.ticket.price + >> @rep_tickets.rp_part[i].price >> end >> > > @rep.ticket.price = @rep_tickets..rp_parts.map(:&price).sum >or even @rep_tickets.rp_parts.sum(:&price) (consider also @rep_tickets.rp_parts.target.sum(:&price) The difference between the two is that the first will always hit the database (ie it will do select sum(price) from ...) whereas the second won''t if rp_parts has already been loaded (assuming rp_parts is an active record association) Fred> > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> On 10 Jan 2008, at 18:23, Russell McConnachie wrote: > >>> >> >> @rep.ticket.price = @rep_tickets..rp_parts.map(:&price).sum >> > > or even @rep_tickets.rp_parts.sum(:&price) > > (consider also @rep_tickets.rp_parts.target.sum(:&price) > The difference between the two is that the first will always hit the > database (ie it will do select sum(price) from ...) whereas the second > won''t if rp_parts has already been loaded (assuming rp_parts is an > active record association) > > FredDoes not work! Repair tickets and rp_parts are HABTM. Help! -- 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 Jan 10, 2008, at 1:34 PM, John Smith wrote:> Russell McConnachie wrote: >> Russell McConnachie wrote: >>> @rep.ticket.price = @rep_tickets..rp_parts.map(:&price).sum >>> >> Wow, fat fingered that. >> >> @rep.ticket.price = @rep_tickets.rp_parts.map(&:price).sum > > Doesn''t seem to work: > >>> @repair_ticket.total_price = >>> @repair_ticket.rp_parts.map(:&price).sum > SyntaxError: compile error > (irb):71: syntax error, unexpected tIDENTIFIER, expecting '')'' > @repair_ticket.total_price = @repair_ticket.rp_parts.map(:&price).sum.map(&:price) Note that & causes the following object, if not already a Proc, to be sent a .to_proc method. Rails defines Symbol#to_proc. Your syntax error is because you have transposed the & and : in the line. -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---