Hi folks,
I’m new to Ruby & Rails, and have been reading the Agile Web dev &
Pickaxe books with much joy!
However, I need some guidance re: using migrations to load in data for
tables that have relationships.
For example, I have one products table and one product_photos table
(defined in separate migration files). The second table has a foreign
key associated with the products table:
execute “alter table product_photos
add constraint fk_products
foreign key (product_id) references products(id)”
The models have the relationships defined:
class Product < ActiveRecord::Base
has_one :product_photo
…
class ProductPhoto < ActiveRecord::Base
belongs_to :product
…
In my data migration file, I create a Product, then a ProductPhoto and
try to link them:
prod1 = Product.create(…)
photo1 = ProductPhoto.create(…) -> has no reference to product_id
prod1.product_photo = photo1
There are no errors after the rake db:migrate command, but examination
of the table reveals the foreign key product_id to be NULL.
Could someone give me a heads up on what I’m doing wrong, or where to go
to get more info on this area. Plus I have other tables with
has_and_belongs_to_many relationships that I haven’t tried to link yet.
I’m not sure how much ruby coding can be done within migrations.
FYI, I’m using InstantRails 1.3a.
Sorry if this has been covered before, but forum search ain’t working
just now.
Thanks,
Karen