Jay McGavren's Journal

2008-07-23

ActiveRecord associations are neat!

class Album < ActiveRecord::Base
	has_many :songs
end
>> album.songs < [#]
>> album
=> #
>> song
=> #

Note that empty album_id…

>> album.save
=> true
>> song
=> #

album_id is populated…

mysql> select * from songs;
+----+----------+-------------+--------------+
| id | album_id | name        | track_number |
+----+----------+-------------+--------------+
|  1 |        8 | First Track |            1 |
+----+----------+-------------+--------------+

And the song is saved as well. Nice to have stuff done for me for once! (Let’s see if my attitude changes when Rails volunteers to do something I didn’t want done.)

comments powered by Disqus