Populating a join table through migrations

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:

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.

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,

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!

Craig, I think this is what I'm looking for. Will post if I have any issues!

Thx!

Raphael