Forum: Ruby on Rails Dynamically iterate around a table's columns (using content_

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F2408335959406f3613bb40d36ff6112?d=identicon&s=25 Nikos D. (demisone)
on 2007-05-01 11:47
(Received via mailing list)
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).
F2408335959406f3613bb40d36ff6112?d=identicon&s=25 Nikos D. (demisone)
on 2007-05-01 13:00
(Received via mailing list)
> 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....
Ben Munat (Guest)
on 2007-05-01 20:26
(Received via mailing list)
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
jfcote (Guest)
on 2007-05-02 15:14
(Received via mailing list)
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)
F2408335959406f3613bb40d36ff6112?d=identicon&s=25 Nikos D. (demisone)
on 2007-05-06 19:05
(Received via mailing list)
> 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 :D

> 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)
JSeidel (Guest)
on 2007-05-06 19:15
(Received via mailing list)
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
This topic is locked and can not be replied to.