加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails-3 – Rails 3工厂女孩多对多的关系

发布时间:2020-12-16 21:15:00 所属栏目:百科 来源:网络整理
导读:目前还没有使用Factory Girl 4.1(我能找到)的最新答案 – 你如何在工厂内建立多对多的关系? 例如,我有使用连接表处于多对多关系的学生和教室,到目前为止,我有以下设置: factory :classroom do name "Foo Class" ...endfactory :student do name "John Doe"
目前还没有使用Factory Girl 4.1(我能找到)的最新答案 – 你如何在工厂内建立多对多的关系?

例如,我有使用连接表处于多对多关系的学生和教室,到目前为止,我有以下设置:

factory :classroom do
    name "Foo Class"
    ...
end

factory :student do
   name "John Doe"
   ...
end

factory :student_with_classroom,:parent => :student do
    after(:build) {|student| student.classrooms << classroom}
end

然而,这会导致:

NameError:
       undefined local variable or method `classroom' for #<FactoryGirl::SyntaxRunner>

我的尝试在很大程度上是猜测,因为我没有运气找到任何未弃用的语法来实现这一目标.

解决方法

实际上,我设法在SO: How to create has_and_belongs_to_many associations in Factory girl中找到了我正在寻找的一些其他答案的答案
factory :classroom do
    name "Foo Class"
    ...
end

factory :student do
   name "John Doe"
   ...
end

factory :student_with_classroom,:parent => :student do
    classrooms {[FactoryGirl.create(:classroom)]}
end

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读