Is it possible to automatically populate a join table with table data through migrations? So far, I have been able to do it with tables as in the following example: ====class CreateCategories < ActiveRecord::Migration def self.up create_table :categories do |t| t.column :title, :string end Category.create(:title => "My first cateegory data") end def self.down drop_table :categories end end ==== How would this migration change if, say, I wanted to create a table called "categories_subcategories" ? I''m able to generate the table, but not populate the table. Perhaps it isn''t possible since I''m not generating a model? If it is possible, what would be the equivalent of the "Category" object in the above example? Thanks in advance, Raphael --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
phirefly wrote:> How would this migration change if, say, I wanted to create a table > called "categories_subcategories" ?This actually sounds like you might want to use acts_as_tree here, or another hierarchical structure.> > I''m able to generate the table, but not populate the table.One of the problems here is how to determine which categories belong to others. I suppose you are storing this in the one table at the moment?> Perhaps it > isn''t possible since I''m not generating a model?I don''t think rails can talk to the database without a model (anyone?). Certainly it would not be as easy without AR.>If it is possible, > what would be the equivalent of the "Category" object in the above > example?Depends on exactly what you want to achieve. As mentioned above you could use acts_as_tree, or you could expose categories via a polymorphic interface and declare has_many and/or belongs to, or you could create a subcategories table with its associated model.> > Thanks in advance, > > Raphael-- 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?hl=en -~----------~----~----~----~------~----~------~--~---
On Tue, 2008-05-06 at 22:18 -0700, phirefly wrote:> Is it possible to automatically populate a join table with table data > through migrations? So far, I have been able to do it with tables as > in the following example: > ====> class CreateCategories < ActiveRecord::Migration > def self.up > create_table :categories do |t| > t.column :title, :string > end > > Category.create(:title => "My first cateegory data") > > end > > def self.down > drop_table :categories > end > end > > ====> > How would this migration change if, say, I wanted to create a table > called "categories_subcategories" ? > > I''m able to generate the table, but not populate the table. Perhaps it > isn''t possible since I''m not generating a model? If it is possible, > what would be the equivalent of the "Category" object in the above > example? > > Thanks in advance,---- I routinely do this kind of thing in migrations for my rights/roles security implementation... @admin = [''client_handbook_addendum'', ''rhba_signoff_form''] @create = [''client_handbook_addendum'', ''rhba_signoff_form''] @admin.each do |a| Right.create :name => "Clients Admin", :controller => "clients", :action => a end @create.each do |b| Right.create :name => "Clients Create", :controller => "clients", :action => b end Right.find(:all, :conditions => ["controller = ? AND action = ?", ''clients'', ''client_handbook_addendum'']).each { |ri| ri.roles << Role.find(:first, :conditions => ["name = ?", ri.name]) } Right.find(:all, :conditions => ["controller = ? AND action = ?", ''clients'', ''rhba_signoff_form'']).each { |ri| ri.roles << Role.find(:first, :conditions => ["name = ?", ri.name]) } which first populates my Rights and then populates my rights_roles join table. Craig --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thanks for the reply! I''ll do more digging. Makes sense that this wouldn''t be possible without a model. I haven''t used acts_as_tree before, so I''ll need to figure that out. Thanks! On May 7, 2:18 am, Andrew Skegg <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> phirefly wrote: > > How would this migration change if, say, I wanted to create a table > > called "categories_subcategories" ? > > This actually sounds like you might want to use acts_as_tree here, or > another hierarchical structure. > > > > > I''m able to generate the table, but not populate the table. > > One of the problems here is how to determine which categories belong to > others. I suppose you are storing this in the one table at the moment? > > > Perhaps it > > isn''t possible since I''m not generating a model? > > I don''t think rails can talk to the database without a model (anyone?). > Certainly it would not be as easy without AR. > > >If it is possible, > > what would be the equivalent of the "Category" object in the above > > example? > > Depends on exactly what you want to achieve. As mentioned above you > could use acts_as_tree, or you could expose categories via a polymorphic > interface and declare has_many and/or belongs to, or you could create a > subcategories table with its associated model. > > > > > Thanks in advance, > > >Raphael > > -- > Posted viahttp://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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Craig, I think this is what I''m looking for. Will post if I have any issues! Thx! Raphael --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---