# How to cycle over all self.xx elements performing a method?

**URL:** <https://www.ruby-forum.com/t/how-to-cycle-over-all-self-xx-elements-performing-a-method/87035>\
**Category:** Rails\
**Created:** [February 27, 2007, 7:17pm UTC](https://www.ruby-forum.com/t/how-to-cycle-over-all-self-xx-elements-performing-a-method/87035 "2007-02-27T19:17:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![scot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/77aa72/32.png) [@scot](https://www.ruby-forum.com/u/scot)\
**Post date:** [February 27, 2007, 7:17pm UTC](https://www.ruby-forum.com/t/how-to-cycle-over-all-self-xx-elements-performing-a-method/87035/1 "2007-02-27T19:17:07Z")

</div>

Hi,

I would like to perform CGI.escapeHTML to all var’s before save,  
somthing like  
(that obviously dont work)  
self.each do |attr|  
attr = CGI.escapeHTML(attr) if attr  
end

Thanks  
Scot

---

<div class="post-metadata">

**Author:** ![scot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/77aa72/32.png) [@scot](https://www.ruby-forum.com/u/scot)\
**Post date:** [February 27, 2007, 7:25pm UTC](https://www.ruby-forum.com/t/how-to-cycle-over-all-self-xx-elements-performing-a-method/87035/2 "2007-02-27T19:25:04Z")

</div>

> I would like to perform CGI.escapeHTML to all var’s before save,

Wouldn’t this be a straightforward application of the before\_save()  
callback in your model(s)?

---

<div class="post-metadata">

**Author:** ![scot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/77aa72/32.png) [@scot](https://www.ruby-forum.com/u/scot)\
**Post date:** [February 27, 2007, 7:30pm UTC](https://www.ruby-forum.com/t/how-to-cycle-over-all-self-xx-elements-performing-a-method/87035/3 "2007-02-27T19:30:35Z")

</div>

Mark T. wrote:

> > I would like to perform CGI.escapeHTML to all var’s before save,
> 
> Wouldn’t this be a straightforward application of the before\_save()  
> callback in your model(s)?

yes, this is how I am currently doing it, maybe I have miss understood  
how before save works

before\_save :encodeTags

# 

#removes html tags and encode &'s etc… CGI.escapeHTML()  
def removeTags  
self.name = strip\_tags(self.name) if self.name  
self.author = strip\_tags(self.author) if self.author  
self.author\_email = strip\_tags(self.author\_email) if  
self.author\_email  
self.url = strip\_tags(self.url) if self.url  
self.repository = strip\_tags(self.repository) if self.repository  
end

I want to be able just to cycle over each attribute without having to  
specify a line for each one.

---

<div class="post-metadata">

**Author:** ![scot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/77aa72/32.png) [@scot](https://www.ruby-forum.com/u/scot)\
**Post date:** [February 27, 2007, 7:41pm UTC](https://www.ruby-forum.com/t/how-to-cycle-over-all-self-xx-elements-performing-a-method/87035/4 "2007-02-27T19:41:06Z")

</div>

Hi Scot,

scot wrote:

> before\_save :encodeTags  
> def removeTags  
> end  
> I assume you recognize the mismatch here and that it’s a typo.

> I want to be able just to cycle over each attribute  
> without having to specify a line for each one.

Scaffold a sandbox app over a table and look at the list method to see  
how  
Rails generates the column headings. That’s how you do it.

hth,  
Bill

---

<div class="post-metadata">

**Author:** ![scot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/77aa72/32.png) [@scot](https://www.ruby-forum.com/u/scot)\
**Post date:** [February 27, 2007, 7:58pm UTC](https://www.ruby-forum.com/t/how-to-cycle-over-all-self-xx-elements-performing-a-method/87035/5 "2007-02-27T19:58:14Z")

</div>

> I want to be able just to cycle over each attribute without having to  
> specify a line for each one.  
> not tested, just writting by heart but i’m pretty sure it should work

self.attributes.each do |key,value|  
self[key] = strip\_tags(self[key]) if self[key]  
end

regards,

javier ramirez

## –

Estamos de estreno… si necesitas llevar el control de tus gastos  
visita [http://www.gastosgem.com](http://www.gastosgem.com) !!Es gratis!!

---

<div class="post-metadata">

**Author:** ![scot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/77aa72/32.png) [@scot](https://www.ruby-forum.com/u/scot)\
**Post date:** [February 27, 2007, 8:03pm UTC](https://www.ruby-forum.com/t/how-to-cycle-over-all-self-xx-elements-performing-a-method/87035/6 "2007-02-27T20:03:15Z")

</div>

# Maybe something like…

attribute\_names.select { |a| attribute\_present? a }.each { |a| self[a]  
= strip\_tags(self[a]) }

---

<div class="post-metadata">

**Author:** ![scot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/77aa72/32.png) [@scot](https://www.ruby-forum.com/u/scot)\
**Post date:** [February 27, 2007, 8:29pm UTC](https://www.ruby-forum.com/t/how-to-cycle-over-all-self-xx-elements-performing-a-method/87035/7 "2007-02-27T20:29:38Z")

</div>

John B. wrote:

> # Maybe something like…
> 
> attribute\_names.select { |a| attribute\_present? a }.each { |a| self[a]  
> = strip\_tags(self[a]) }

thanks everyone, this is how I did it in the end, however teh above  
solution seems to look nice too.

# 

# add new lines as required

# CGI.escapeHTML() or ERB::Util::html\_escape

def encodeTags

string\_attributes = {}  
self.attributes.each do |each\_name, each\_attribute|  
if each\_attribute.kind\_of? String  
string\_attributes[each\_name] =  
ERB::Util::html\_escape(each\_attribute)  
end  
end  
self.attributes = string\_attributes

end
