Karen Jenkin
2006-Jul-18 07:40 UTC
[Rails] newbie help re: loading data and relationships in migrations
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 -- Posted via http://www.ruby-forum.com/.
Chris Parsons
2006-Jul-18 08:04 UTC
[Rails] Re: newbie help re: loading data and relationships in migrations
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On 18/07/2006 08:40, Karen Jenkin wrote:> 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 = photo1Try a prod1.save! after that line...? Chris -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2.1 (Cygwin) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFEvJXtsf37utvsUfsRAqo5AKCLDhOh5q/qmHTCNPGg7I9jmPKobgCfUwgC ljcsqPuRqihGGUscnpna8Oo=4RpN -----END PGP SIGNATURE-----