Can ActiveRecord dynamically add a column to a table? I want the application to add a column to a second table when a new record in the main table is created, with the name as a field in the main table.
heh. DONT do this. tables has a max horizontal size limit. and from what your doing is going to get hit very quickly. you might want to check out the module acts_as_verical http://www.tuxsoft.se/oss/rails/acts_as_vertical or if your truly trying to make a matrix, id look more toward a habtm style design. On 8/10/05, Christian Carter <cdcarter-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Can ActiveRecord dynamically add a column to a table? I want the > application to add a column to a second table when a new record in > the main table is created, with the name as a field in the main table. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Zachery Hostens <zacheryph-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
On 8/10/05, Christian Carter <cdcarter-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Can ActiveRecord dynamically add a column to a table? I want the > application to add a column to a second table when a new record in > the main table is created, with the name as a field in the main table.You know, I can''t think why in god''s name you''d want this :) But, you can use the Connection Adapter for this: http://rails.rubyonrails.com/classes/ActiveRecord/ConnectionAdapters/AbstractAdapter.html Model.connection.add_column :my_table, :my_field, :integer -- rick http://techno-weenie.net
I need it for an old database system, that I am trying to add a web interface to. There are two tables, the programs table, and the users table, for every entry in the programs table, there is a column in the users table set to either 0/1 signifying if they have it or not. If anybody has any suggestions for a new database structure, that would still offer full functionality, please tell me> > On 8/10/05, Christian Carter <cdcarter-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> Can ActiveRecord dynamically add a column to a table? I want the >> application to add a column to a second table when a new record in >> the main table is created, with the name as a field in the main >> table. >> > > You know, I can''t think why in god''s name you''d want this :) > > But, you can use the Connection Adapter for this: > http://rails.rubyonrails.com/classes/ActiveRecord/ > ConnectionAdapters/AbstractAdapter.html > > Model.connection.add_column :my_table, :my_field, :integer > > -- > rick > http://techno-weenie.net > >
Sure: Table: table Programs ---- id name other program info table users ---- id name login email other user info table users_programs id user_id program_id then with rails, users has a has_and_belongs_to_many programs, and programs has a has_and_belongs_to_many users relationship. If the user has the program, join them. If not, don''t. That should do everything you need it to. caleb On 8/11/05, Christian Carter <cdcarter-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I need it for an old database system, that I am trying to add a web > interface to. There are two tables, the programs table, and the > users table, for every entry in the programs table, there is a column > in the users table set to either 0/1 signifying if they have it or > not. If anybody has any suggestions for a new database structure, > that would still offer full functionality, please tell me > > > > > > On 8/10/05, Christian Carter <cdcarter-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > >> Can ActiveRecord dynamically add a column to a table? I want the > >> application to add a column to a second table when a new record in > >> the main table is created, with the name as a field in the main > >> table. > >> > > > > You know, I can''t think why in god''s name you''d want this :) > > > > But, you can use the Connection Adapter for this: > > http://rails.rubyonrails.com/classes/ActiveRecord/ > > ConnectionAdapters/AbstractAdapter.html > > > > Model.connection.add_column :my_table, :my_field, :integer > > > > -- > > rick > > http://techno-weenie.net > > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- caleb http://www.ruejulesverne.com
Ok, I understood most of that, except the join them bit. How do I join them?> Sure: > > Table: > > table Programs > ---- > id > name > other program info > > table users > ---- > id > name > login > email > other user info > > table users_programs > id > user_id > program_id > > then with rails, users has a has_and_belongs_to_many programs, and > programs has a has_and_belongs_to_many users relationship. > > If the user has the program, join them. If not, don''t. > > That should do everything you need it to. > > caleb > On 8/11/05, Christian Carter <cdcarter-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> I need it for an old database system, that I am trying to add a web >> interface to. There are two tables, the programs table, and the >> users table, for every entry in the programs table, there is a column >> in the users table set to either 0/1 signifying if they have it or >> not. If anybody has any suggestions for a new database structure, >> that would still offer full functionality, please tell me >> >> >> >>> >>> On 8/10/05, Christian Carter <cdcarter-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >>> >>> >>>> Can ActiveRecord dynamically add a column to a table? I want the >>>> application to add a column to a second table when a new record in >>>> the main table is created, with the name as a field in the main >>>> table. >>>> >>>> >>> >>> You know, I can''t think why in god''s name you''d want this :) >>> >>> But, you can use the Connection Adapter for this: >>> http://rails.rubyonrails.com/classes/ActiveRecord/ >>> ConnectionAdapters/AbstractAdapter.html >>> >>> Model.connection.add_column :my_table, :my_field, :integer >>> >>> -- >>> rick >>> http://techno-weenie.net >>> >>> >>> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> > > > -- > caleb > http://www.ruejulesverne.com > > >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
In SQL, you''d add a row to the join table. In rails, for example, you''d have data like this: programs: ID: 1 Name: Excel Vendor: Microsoft ID: 2 Name: Konquerer Vendor: KDE users: ID: 1 name: caleb login: crutan email: crutan-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org ID: 2 name: chrisitan login: cdcarter email: cdcarter-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org users_programs user_id: 1 program_id: 1 user_id: 1 program_id: 2 user_id: 2 program_id: 1 So there are two programs, Excel and Konquerer in the db, and my user has access to both of them. Your user does not. To cycle through my users available programs, you''d do this: user = User.Find(1) user.programs.each { do something } To make user 2 have access to conquerer, we need to add a row to the join table. Rails will do this for us, if you were to do the following: user = User.Find(2) user.programs<<(Program.Find(2)) This stuff is all in the RDOCs for ActiveRecord::Associations::ClassMethods You can read all the docs for it here: http://api.rubyonrails.com/classes/ActiveRecord/Aggregations/ClassMethods.html caleb On 8/11/05, Christian Carter <cdcarter-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > Ok, I understood most of that, except the join them bit. How do I join > them? > > > > Sure: > > > > > Table: > > > > > table Programs > > ---- > > id > > name > > other program info > > > > > table users > > ---- > > id > > name > > login > > email > > other user info > > > > > table users_programs > > id > > user_id > > program_id > > > > > then with rails, users has a has_and_belongs_to_many programs, and > > programs has a has_and_belongs_to_many users relationship. > > > > > If the user has the program, join them. If not, don''t. > > > > > That should do everything you need it to. > > > > > caleb > > On 8/11/05, Christian Carter <cdcarter-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I need it for an old database system, that I am trying to add a web > > interface to. There are two tables, the programs table, and the > > users table, for every entry in the programs table, there is a column > > in the users table set to either 0/1 signifying if they have it or > > not. If anybody has any suggestions for a new database structure, > > that would still offer full functionality, please tell me > > > > > > > > > > > > On 8/10/05, Christian Carter <cdcarter-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > Can ActiveRecord dynamically add a column to a table? I want the > > application to add a column to a second table when a new record in > > the main table is created, with the name as a field in the main > > table. > > > > > > > > > You know, I can''t think why in god''s name you''d want this :) > > > > > But, you can use the Connection Adapter for this: > > http://rails.rubyonrails.com/classes/ActiveRecord/ > > ConnectionAdapters/AbstractAdapter.html > > > > > Model.connection.add_column :my_table, :my_field, :integer > > > > > -- > > rick > > http://techno-weenie.net > > > > > > > > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > > > > -- > > caleb > > http://www.ruejulesverne.com > > > > > > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-- caleb http://www.ruejulesverne.com
Or rather, here: http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html Sorry, wrong link. All the rails api docs are at http://api.rubyonrails.com/ caleb On 8/11/05, Caleb Rutan <crutan-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> In SQL, you''d add a row to the join table. > > In rails, for example, you''d have data like this: > > programs: > ID: 1 > Name: Excel > Vendor: Microsoft > > ID: 2 > Name: Konquerer > Vendor: KDE > > users: > ID: 1 > name: caleb > login: crutan > email: crutan-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > > ID: 2 > name: chrisitan > login: cdcarter > email: cdcarter-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > > users_programs > user_id: 1 > program_id: 1 > > user_id: 1 > program_id: 2 > > user_id: 2 > program_id: 1 > > So there are two programs, Excel and Konquerer in the db, and my user > has access to both of them. Your user does not. > > To cycle through my users available programs, you''d do this: > > user = User.Find(1) > user.programs.each { do something } > > To make user 2 have access to conquerer, we need to add a row to the > join table. Rails will do this for us, if you were to do the > following: > > user = User.Find(2) > user.programs<<(Program.Find(2)) > > This stuff is all in the RDOCs for ActiveRecord::Associations::ClassMethods > > You can read all the docs for it here: > http://api.rubyonrails.com/classes/ActiveRecord/Aggregations/ClassMethods.html > > caleb > > > > > On 8/11/05, Christian Carter <cdcarter-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > Ok, I understood most of that, except the join them bit. How do I join > > them? > > > > > > > > Sure: > > > > > > > > > > Table: > > > > > > > > > > table Programs > > > > ---- > > > > id > > > > name > > > > other program info > > > > > > > > > > table users > > > > ---- > > > > id > > > > name > > > > login > > > > email > > > > other user info > > > > > > > > > > table users_programs > > > > id > > > > user_id > > > > program_id > > > > > > > > > > then with rails, users has a has_and_belongs_to_many programs, and > > > > programs has a has_and_belongs_to_many users relationship. > > > > > > > > > > If the user has the program, join them. If not, don''t. > > > > > > > > > > That should do everything you need it to. > > > > > > > > > > caleb > > > > On 8/11/05, Christian Carter <cdcarter-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > I need it for an old database system, that I am trying to add a web > > > > interface to. There are two tables, the programs table, and the > > > > users table, for every entry in the programs table, there is a column > > > > in the users table set to either 0/1 signifying if they have it or > > > > not. If anybody has any suggestions for a new database structure, > > > > that would still offer full functionality, please tell me > > > > > > > > > > > > > > > > > > > > > > > > On 8/10/05, Christian Carter <cdcarter-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > > > > > > Can ActiveRecord dynamically add a column to a table? I want the > > > > application to add a column to a second table when a new record in > > > > the main table is created, with the name as a field in the main > > > > table. > > > > > > > > > > > > > > > > > > You know, I can''t think why in god''s name you''d want this :) > > > > > > > > > > But, you can use the Connection Adapter for this: > > > > http://rails.rubyonrails.com/classes/ActiveRecord/ > > > > ConnectionAdapters/AbstractAdapter.html > > > > > > > > > > Model.connection.add_column :my_table, :my_field, :integer > > > > > > > > > > -- > > > > rick > > > > http://techno-weenie.net > > > > > > > > > > > > > > > > > > _______________________________________________ > > > > Rails mailing list > > > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > > > > > > > > > > > > > > > > -- > > > > caleb > > > > http://www.ruejulesverne.com > > > > > > > > > > > > > > > > > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > -- > caleb > http://www.ruejulesverne.com >-- caleb http://www.ruejulesverne.com