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:

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

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

On Tue, 2008-05-06 at 22:18 -0700, phirefly wrote:

Category.create(:title => "My first cateegory data")

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

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

Thx!

Raphael

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 S. [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs