# Help on writing delete\_if on my hash?

**URL:** <https://www.ruby-forum.com/t/help-on-writing-delete-if-on-my-hash/104018>\
**Category:** Rails\
**Created:** [May 12, 2007, 11:51pm UTC](https://www.ruby-forum.com/t/help-on-writing-delete-if-on-my-hash/104018 "2007-05-12T23:51:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mixplate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@mixplate](https://www.ruby-forum.com/u/mixplate)\
**Post date:** [May 12, 2007, 11:51pm UTC](https://www.ruby-forum.com/t/help-on-writing-delete-if-on-my-hash/104018/1 "2007-05-12T23:51:31Z")

</div>

Hi,  
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?

---

<div class="post-metadata">

**Author:** ![mixplate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@mixplate](https://www.ruby-forum.com/u/mixplate)\
**Post date:** [May 13, 2007, 12:05am UTC](https://www.ruby-forum.com/t/help-on-writing-delete-if-on-my-hash/104018/2 "2007-05-13T00:05:38Z")

</div>

@temp.delete\_if {|key, ‘apprflg’| apprflg == ‘1’ }] ?

---

<div class="post-metadata">

**Author:** ![mixplate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@mixplate](https://www.ruby-forum.com/u/mixplate)\
**Post date:** [May 13, 2007, 12:08am UTC](https://www.ruby-forum.com/t/help-on-writing-delete-if-on-my-hash/104018/3 "2007-05-13T00:08:35Z")

</div>

actually its this right?

@temp.delete\_if {|x| x.apprflg == 0}

---

<div class="post-metadata">

**Author:** ![mixplate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@mixplate](https://www.ruby-forum.com/u/mixplate)\
**Post date:** [May 14, 2007, 2:01am UTC](https://www.ruby-forum.com/t/help-on-writing-delete-if-on-my-hash/104018/4 "2007-05-14T02:01:45Z")

</div>

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 Y.

> **[Pete Yandell — Principal Engineer / Architect](https://notahat.com/)**
>
> Pete Yandell is an experienced Principal Software Engineer and Architect based in Melbourne, Australia.
