Hi, im new to Ruby on Rails and im trying to do the following i = 1 @elements = Post.find(:all) @elements.each do |el| if el.name=="test" # remove el from @elements end end How can i achieve this? i was trying to do el.delete but that is going to delete the element from the database and i just want the element to be removed from current listing. im implementing a search functionality and i want to filter the results from Post.find(:all). In the real app i cant do it by sql. Thanks -- 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-/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.
Phil On Sat, Feb 19, 2011 at 6:47 PM, Juan manuel V. <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>wrote:> Hi, im new to Ruby on Rails and im trying to do the following > > i = 1 > @elements = Post.find(:all) > @elements.each do |el| > if el.name=="test" > # remove el from @elements > end > end > > > How can i achieve this? i was trying to do > el.delete but that is going to delete the element from the database > and i just want the element to be removed from current listing. im > implementing a search functionality and i want to filter the results > from Post.find(:all). In the real app i cant do it by sql. >There''s more than one way. The first one that comes to mind is just the difference of 2 arrays: @elements = @elements - [el] #@elements will now equal itself, minus the element that matched ''test''> Thanks > > -- > 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-/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. > >-- 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.
Hassan Schroeder
2011-Feb-20 01:24 UTC
Re: How to delete a single item here.cant be so hard!
On Sat, Feb 19, 2011 at 4:47 PM, Juan manuel V. <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> @elements = Post.find(:all) > @elements.each do |el| > if el.name=="test" > # remove el from @elements > end > endFirst, it would be more readable for you or another maintainer down the road to write this as @posts = Post.all @posts.each do |post|> How can i achieve this? i was trying to do > el.delete but that is going to delete the element from the database > and i just want the element to be removed from current listing.@posts is an Array; look at the rdoc for methods available. And finally, you could replace the .each loop with one line: @posts.delete( Post.find_by_name("test") ) HTH! -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org twitter: @hassan -- 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.
Jim Ruther Nill
2011-Feb-20 03:17 UTC
Re: How to delete a single item here.cant be so hard!
On Sun, Feb 20, 2011 at 8:47 AM, Juan manuel V. <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>wrote:> Hi, im new to Ruby on Rails and im trying to do the following > > i = 1 > @elements = Post.find(:all) > @elements.each do |el| > if el.name=="test" > # remove el from @elements > end > end > >from http://www.ruby-doc.org/core/classes/Array.html#M000259 @elements.reject! {|el| el.name == ''test''}> > How can i achieve this? i was trying to do > el.delete but that is going to delete the element from the database > and i just want the element to be removed from current listing. im > implementing a search functionality and i want to filter the results > from Post.find(:all). In the real app i cant do it by sql. > Thanks > > -- > 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-/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. > >-- ------------------------------------------------------------- visit my blog at http://jimlabs.heroku.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-/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.