开发者

Fixtures with associations in rails

i am getting some problem in Fixtures

I am having models advantage and kind

advantage.rb

belongs_to :kind

kind.rb

 has_many :advantages

advantages.yml

1.

id: 1
title: something
kind: apple
kind_id: 1

2.

id: 2
title: somethjin
kind: orange
kind_id: 2

kinds.yml

apple:
  id: 1
  name: apple
orange:
  id: 2
  name: orange

I am trying to bring association here as kind_id is coming with some junk values

so i did like

kinds.yml

apple:
开发者_高级运维  name: apple
orange:
  name: orange

and in advantages.yml

something:

title: something
kind: apple

somethjin

title: somethjin
kind: orange

But it didn't work

How to resolve this


Try like this but not sure:

kinds.yml
 apple:
  id: 1
  name: 'name1'
 orange:
  id: 2
  name: 'name2'

advantages.yml
 adv1:
  id: 1
  title: 'title1'
  kind: apple


It is not an answer for your question. But i always use Factories instead of fixtures. It is much easier to maintain and associations are easy.

Take a look here: https://github.com/thoughtbot/factory_girl

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜