Hello. I have a table that will have a changing number of columns. In order to update them, I need to able to keep my column names in a variable, and pass them to my model object. Such as: @entry = Model.create @columns.each do |column| @entry.column = ''something'' end Obviously, this doesn''t work. Is there any method in ActiveRecord that would allow me to pass a string as the column name? ie: @entry.put_in_column(:name, :value) Or is there a better way to handle a changing set of columns? Thanks. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> I have a table that will have a changing number of columns. In order to > update them, I need to able to keep my column names in a variable, and > pass them to my model object. Such as: > > @entry = Model.create > @columns.each do |column| > @entry.column = ''something'' > end > > Obviously, this doesn''t work. Is there any method in ActiveRecord that > would allow me to pass a string as the column name? ie: > > @entry.put_in_column(:name, :value) > > Or is there a better way to handle a changing set of columns?Maybe... update_attribute or update_attributes? http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M000909 --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
I would guess update_attributes is best as update_attribute would cause a database query for each column. That way he would just build a hash of column names and values... -matthew -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hey, are you saying that the table schema changes during the active lifetime of your rails application? If not then I''ve misunderstood you and the rest of this response is not relevant. My first reaction would be to avoid messing with the underlying schema at runtime... It''s not an informed reaction, it just strikes me as bad news. If schema changes happen so frequently that you can''t afford the downtime of a deploy I''d suggest you were abusing the relational model - time to rethink your design. If schema changes are not happening that frequently then things will be a lot more stable if you redeploy with each schema change. Of course, that''s just one guy''s opinion. However, if changing the schema is what you''re set on doing then you *might* have some success calling Model.reset_column_information as you think is required - however, rails autogenerates methods based on the schema and I don''t know enough about that end of things to be able to say whether it''ll work at all - just a suggestion. Regards, Trevor On 10/3/06, Bryan M <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Hello. > > I have a table that will have a changing number of columns. In order to > update them, I need to able to keep my column names in a variable, and > pass them to my model object. Such as: > > @entry = Model.create > @columns.each do |column| > @entry.column = ''something'' > end > > Obviously, this doesn''t work. Is there any method in ActiveRecord that > would allow me to pass a string as the column name? ie: > > @entry.put_in_column(:name, :value) > > Or is there a better way to handle a changing set of columns? > > Thanks. > > -- > Posted via http://www.ruby-forum.com/. > > > >-- -- Trevor Squires http://somethinglearned.com --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thanks for the replies. I''ll see how well your suggestions work, but Trevor does have a valid point. I am certainly abusing the design model. My intention was to make the app as flexible and scalable as possible (hence changing the schema). I''ve already had to redesign the model a few times, and I''m trying really hard not to break the logic for the parts that are already working smoothly. But you''re right, I probably should redesign. DBs are just not an area of expertise for me, so its a constant learning process. Thanks again, Bryan -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---