# Dynamically iterate around a table's columns (using content\_

**URL:** <https://www.ruby-forum.com/t/dynamically-iterate-around-a-tables-columns-using-content/103065>\
**Category:** Rails\
**Created:** [May 1, 2007, 11:47am UTC](https://www.ruby-forum.com/t/dynamically-iterate-around-a-tables-columns-using-content/103065 "2007-05-01T11:47:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![demisone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/977dab/32.png) [@demisone](https://www.ruby-forum.com/u/demisone)\
**Post date:** [May 1, 2007, 11:47am UTC](https://www.ruby-forum.com/t/dynamically-iterate-around-a-tables-columns-using-content/103065/1 "2007-05-01T11:47:45Z")

</div>

Hi there,  
I was trying to make some DRY code in the ‘edit’ controller so it will  
check for every single column and if it is empty, to write something  
like ‘[Undefined]’ in it’s place. The thing is didn’t want to make the  
code static and have to rewrite it every time i change the table it  
uses, so i thought that this (or something like this) would work:

def edit  
@post = Post.find(params[:id])  
for column in Post.content\_columns  
if @post.column = ‘’  
@post.column = “[#{column.human\_name} not set]”  
end  
end  
end

Also, i found weird the fact that (in other cases not here - as this  
code won’t work) if i use the condition @post.\<some\_column\_here\> = nil  
doesn’t seem to work (when the selected column is null, meaning not  
set in model creation). Instead i used the = ‘’ (an empty sting) and  
worked ok until now, but i believe that this is reaaally bad coding  
and a hole of bugs in the future…  
Isn’t there any better way to make this work?

Any help would be highly appreciated 🙂

P.S. : As you can see (probably) i’m pretty noob in rails when it  
comes to practical experience, although i’ve read a lot documentation  
and books about rails (and ruby).

---

<div class="post-metadata">

**Author:** ![demisone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/977dab/32.png) [@demisone](https://www.ruby-forum.com/u/demisone)\
**Post date:** [May 1, 2007, 1:00pm UTC](https://www.ruby-forum.com/t/dynamically-iterate-around-a-tables-columns-using-content/103065/2 "2007-05-01T13:00:50Z")

</div>

> Also, i found weird the fact that (in other cases not here - as this  
> code won’t work) if i use the condition @post.\<some\_column\_here\> = nil  
> doesn’t seem to work (when the selected column is null, meaning not  
> set in model creation). Instead i used the = ‘’ (an empty sting) and  
> worked ok until now, but i believe that this is reaaally bad coding  
> and a hole of bugs in the future…  
> Isn’t there any better way to make this work?

i found out why this is happening so nevermind about this one. stupid  
scaffolding…

---

<div class="post-metadata">

**Author:** ![demisone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/977dab/32.png) [@demisone](https://www.ruby-forum.com/u/demisone)\
**Post date:** [May 1, 2007, 8:26pm UTC](https://www.ruby-forum.com/t/dynamically-iterate-around-a-tables-columns-using-content/103065/3 "2007-05-01T20:26:40Z")

</div>

Glad you figured it out. Couple thoughts to chew on though:

“@post.column” would not use the contents of the variable “column” to  
look for a column with that name… it tries to call a “column” method  
on the object @post.

Also, the logic determining whether a model gets some default value  
should really be in the model… and then you can use the protected  
“write\_attribute” method, perhaps in one of the lifecycle callbacks.

b

---

<div class="post-metadata">

**Author:** ![demisone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/977dab/32.png) [@demisone](https://www.ruby-forum.com/u/demisone)\
**Post date:** [May 6, 2007, 7:05pm UTC](https://www.ruby-forum.com/t/dynamically-iterate-around-a-tables-columns-using-content/103065/4 "2007-05-06T19:05:57Z")

</div>

> I moved the code to loop on the table column in the model itself and  
> used the @attributes hash directly to access the columns.
> 
> [‘col1’,‘col2’,‘col3’].each {|col| @attributes[col]=params[col]}

You mean you write [‘col1’,…,] statically? That’s no good for what i  
wanted to to… ☹

> I never got something like self.#{col} to work.

Yeah, me neither 😃

> For now, I am happy with using @attributes[var]. Does anyone have a  
> better solution?

I’m not aware of this (i think), gonna check it out…

Thanks for sharing your thoughts anyway 🙂

-Nikos D (Athens, Greece)

---

<div class="post-metadata">

**Author:** ![demisone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/977dab/32.png) [@demisone](https://www.ruby-forum.com/u/demisone)\
**Post date:** [May 6, 2007, 7:15pm UTC](https://www.ruby-forum.com/t/dynamically-iterate-around-a-tables-columns-using-content/103065/5 "2007-05-06T19:15:58Z")

</div>

I think what you want to code is :

```
 post[column]

```

This uses the [] accessor method and treats ‘column’ as a variable and  
thus gets the correct (dynamic) column that you want to process.

HTH…jon

---

<div class="post-metadata">

**Author:** ![demisone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/977dab/32.png) [@demisone](https://www.ruby-forum.com/u/demisone)\
**Post date:** [May 2, 2007, 3:14pm UTC](https://www.ruby-forum.com/t/dynamically-iterate-around-a-tables-columns-using-content/103065/6 "2007-05-02T15:14:17Z")

</div>

I have been trying to do the same thing.  
What I ended doing is the following. I would be interested to find out  
if I have offended many guidelines in doing so.

I moved the code to loop on the table column in the model itself and  
used the @attributes hash directly to access the columns.

[‘col1’,‘col2’,‘col3’].each {|col| @attributes[col]=params[col]}

I never got something like self.#{col} to work.

For now, I am happy with using @attributes[var]. Does anyone have a  
better solution?

J-F (Ottawa,Canada)
