Rails宝典八十一式:Rails2.0之Fixtures尝鲜

Rails 2.0中Fixtures简单多了,看个例子:
# products.yml
couch:
  name: Couch
  price: 399.99
  manufacturer: lazyboy
  categories: furniture
tv_stand:
  name: TV Stand
  price: 149.95
  manufacturer: highdeph
  categories: furniture, electronics

# manufacturers.yml
lazyboy:
  name: LazyBoy
highdeph:
  name: HighDeph

# categories.yml
furniture:
  name: Furniture
electronics:
  name: Electronics

可以看出:
1,不用再给每条记录指定id了,系统自动添加一个fixture name的integer hash值
2,不用再给created_xx和updated_xx指定值了,自动由Time.now填充
3,当引用其他记录的id时,我们只需指定fixture name即可,而不用指定外键id了

你可能感兴趣的:(Rails)