Forum: Ruby on Rails help on writing delete_if on my hash?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
633ada55ad6d9831e08a6ae23270baf4?d=identicon&s=25 mixplate (Guest)
on 2007-05-12 23:51
i am missing something here....

   @temp = Mymodel.find_by_contents("#{session[:searchstr]}")

i now want to remove all the models that have a field called apprflg
that is set to 1.

@temp.delete_if {|key, value| key == '0' }]

but how do i make it so key is talkking abuoot  my field called apprflg?
633ada55ad6d9831e08a6ae23270baf4?d=identicon&s=25 mixplate (Guest)
on 2007-05-13 00:05

@temp.delete_if {|key, 'apprflg'| apprflg == '1' }]   ?
633ada55ad6d9831e08a6ae23270baf4?d=identicon&s=25 mixplate (Guest)
on 2007-05-13 00:08
actually its this right?

@temp.delete_if {|x| x.apprflg == 0}
C9c7a65848f13e2b1a226bbe43bc3672?d=identicon&s=25 Pete Yandell (pete)
on 2007-05-14 02:01
(Received via mailing list)
On 13/05/2007, at 7:51 AM, mixplate wrote:
> i am missing something here....
>    @temp = Mymodel.find_by_contents("#{session[:searchstr]}")

First of all, shed the quotes. You don't need them:

     @temp = Mymodel.find_by_contents(session[:searchstr])

Second, you're not getting all models matching your search string,
you're only getting one. You want:

     @temp = Mymodel.find_all_by_contents(session[:searchstr])

Third, rather than deleting the records from the array after finding
them, just don't find them in the first place:

     @temp = Mymodel.find(:all, :conditions => ['contents = ? AND
apprflag <> 0', session[:searchstr])

And finally, your variable naming is terrible! Don't abbreviate! What
the hell is an apprflag? :)

Pete Yandell
This topic is locked and can not be replied to.