demisone
2007-May-01 09:47 UTC
Dynamically iterate around a table''s columns (using content_columns?) to save some fields
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). --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
demisone
2007-May-01 10:59 UTC
Re: Dynamically iterate around a table''s columns (using content_columns?) to save some fields
> 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.... --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ben Munat
2007-May-01 18:25 UTC
Re: Dynamically iterate around a table''s columns (using content_columns?) to save some fields
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 demisone wrote:> >> 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.... > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
jfcote
2007-May-02 13:13 UTC
Re: Dynamically iterate around a table''s columns (using content_columns?) to save some fields
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) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Nikos D.
2007-May-06 17:05 UTC
Re: Dynamically iterate around a table''s columns (using content_columns?) to save some fields
> 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) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
JSeidel
2007-May-06 17:15 UTC
Re: Dynamically iterate around a table''s columns (using content_columns?) to save some fields
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 On May 6, 10:05 am, "Nikos D." <demis...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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)--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---