Help on writing delete_if on my hash?

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?

@temp.delete_if {|key, ‘apprflg’| apprflg == ‘1’ }] ?

actually its this right?

@temp.delete_if {|x| x.apprflg == 0}

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? :slight_smile:

Pete Y.
http://notahat.com/

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs