Dynamically iterate around a table's columns (using content_


#1

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

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


#2

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…


#3

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


#4

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… :frowning:

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

Yeah, me neither :smiley:

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

-Nikos D (Athens, Greece)


#5

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


#6

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)