mikebind
2008-Jul-30 08:34 UTC
possible to add columns to a database table from a controller? (not from a migration)
I am trying to figure out how to add columns to a database table from within a controller. I have a client who wants to be able to add their own attributes to a model from within the running application, so I don''t think using migrations is an option. Basically, I have a Person model which starts off with some basic attributes (like first_name and last_name), and my client wants to be able to add new attributes on the fly through the application (like birthdate or hometown, or anything he can think of). My plan for implementing this is to use another model (PersonField) to keep track of each new attribute''s name, label, data type, and default value, and then tie the creation, updating, or destruction of these PersonField objects to the addition, changing, or removing of columns to the ''people'' table in the database. To restate it in another way, I''m looking for the equivalent to ''add_column'' in a migration, but I''d like to call it from a controller. I have not been able to find a solution to this problem, but I''m relatively new to Rails (just a few months experience), so I may not be looking in the right places. I''d appreciate any help or suggestions anyone can give me. Thanks for reading this! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Miles Georgi
2008-Jul-30 20:41 UTC
Re: possible to add columns to a database table from a controller? (not from a migration)
You could run the execute method and pass it the query that creates the column I''d personally create a table to hold these attributes, and add rows to it, rather than creating/dropping columns dynamically. That is I''d create an attribute model and give it a foreign key to the row in the other table, the name, and the value (and possibly a column for each possible type + a type column.) On Wed, Jul 30, 2008 at 1:34 AM, mikebind <mikebind-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I am trying to figure out how to add columns to a database table from > within a controller. I have a client who wants to be able to add > their own attributes to a model from within the running application, > so I don''t think using migrations is an option. > > Basically, I have a Person model which starts off with some basic > attributes (like first_name and last_name), and my client wants to be > able to add new attributes on the fly through the application (like > birthdate or hometown, or anything he can think of). My plan for > implementing this is to use another model (PersonField) to keep track > of each new attribute''s name, label, data type, and default value, and > then tie the creation, updating, or destruction of these PersonField > objects to the addition, changing, or removing of columns to the > ''people'' table in the database. > > To restate it in another way, I''m looking for the equivalent to > ''add_column'' in a migration, but I''d like to call it from a > controller. > > I have not been able to find a solution to this problem, but I''m > relatively new to Rails (just a few months experience), so I may not > be looking in the right places. > > I''d appreciate any help or suggestions anyone can give me. Thanks for > reading this! > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Christopher Kintner
2008-Jul-30 20:45 UTC
Re: possible to add columns to a database table from a controller? (not from a migration)
On Wed, Jul 30, 2008 at 3:34 AM, mikebind <mikebind-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I am trying to figure out how to add columns to a database table from > within a controller. I have a client who wants to be able to add > their own attributes to a model from within the running application, > so I don''t think using migrations is an option. > > Basically, I have a Person model which starts off with some basic > attributes (like first_name and last_name), and my client wants to be > able to add new attributes on the fly through the application (like > birthdate or hometown, or anything he can think of). My plan for > implementing this is to use another model (PersonField) to keep track > of each new attribute''s name, label, data type, and default value, and > then tie the creation, updating, or destruction of these PersonField > objects to the addition, changing, or removing of columns to the > ''people'' table in the database. > > To restate it in another way, I''m looking for the equivalent to > ''add_column'' in a migration, but I''d like to call it from a > controller.You can always execute arbitrary sql commands by using Model.find_by_sql A better way may be to store these customs fields in the database: Person has_many :person_fields PersonField belongs_to :custom_field belongs_to :person CustomField has_many :person_fields> I have not been able to find a solution to this problem, but I''m > relatively new to Rails (just a few months experience), so I may not > be looking in the right places. > > I''d appreciate any help or suggestions anyone can give me. Thanks for > reading this! > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
poecide-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2008-Jul-31 13:19 UTC
Re: possible to add columns to a database table from a controller? (not from a migration)
The reason there''s no easy method for doing what you want to do is because it''s a terrible idea! Letting the customer arbitrarily change the structure of the database is guaranteed to get you an unstable database. You already have a second table in mind to store data about the arbitrary fields the customer wants to create. Have a third table that stores the data and includes pointers back to the attribute table and the persons table. You can set this sort of thing up easily with a has_many :through relationship. This is much more stable and requires no special programming. The customer can create or delete data fields on the fly, use them inconsistently or not at all, and the data remain easily accessible. On Jul 30, 4:34 am, mikebind <mikeb...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I am trying to figure out how to add columns to a database table from > within a controller. I have a client who wants to be able to add > their own attributes to a model from within the running application, > so I don''t think using migrations is an option. > > Basically, I have a Person model which starts off with some basic > attributes (like first_name and last_name), and my client wants to be > able to add new attributes on the fly through the application (like > birthdate or hometown, or anything he can think of). My plan for > implementing this is to use another model (PersonField) to keep track > of each new attribute''s name, label, data type, and default value, and > then tie the creation, updating, or destruction of these PersonField > objects to the addition, changing, or removing of columns to the > ''people'' table in the database. > > To restate it in another way, I''m looking for the equivalent to > ''add_column'' in a migration, but I''d like to call it from a > controller. > > I have not been able to find a solution to this problem, but I''m > relatively new to Rails (just a few months experience), so I may not > be looking in the right places. > > I''d appreciate any help or suggestions anyone can give me. Thanks for > reading this!--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---