views:

27

answers:

3

I have

add_column :foos, :bar_id, :integer

but I wast to be able to do stuff like

@foo.bar.name

rather than

Bar.find(@foo.bar_id)

I think its done with t.references when creating a table... but how do a add it post-table-creating?

A: 

Why do you need to do that in a migration? Why can't you just add the bar_id field in the migration, than add an association in foo's model?

smnirven
+1  A: 

You have to edit corresponding model file and add there for example belongs_to declaration. This is exactly what allows you to call the @foo.bar.name.

Rekin
+1  A: 

see migration guide here.

create_table :products do |t|
  t.references :category
end

That lets you specify it in the migration, but you also need to use *belongs_to*, and either *has_one* or *has_many* attributes in the model. You could really do that without the migration to create it since you already have the foreign key id already setup. Just use the above mentioned attributes.

Also see this ActiveRecord guide.

joshc