[英]HABTM Association with FactoryGirl
Have looked through and tried most examples and still cannot create a working HABTM . 已经查看并尝试了大多数示例,仍然无法创建有效的HABTM 。 No matter how the CaseTrack
and CaseTrackValue
factories are constructed, I'm not able to find the CaseTrackValue[]
in CaseTrack. 无论CaseTrack
和CaseTrackValue
工厂是如何构建的,我都无法在CaseTrack中找到CaseTrackValue[]
。 Shouldn't creating CaseTrack properly provide a CaseTrackValue param within CaseTrack. 不应该在CaseTrack中正确创建CaseTrack提供CaseTrackValue参数。
BTW: the only working association for HABTM seems to be putting 顺便说一句:HABTM的唯一工作协会似乎正在推出
case_track_values { |a| [a.association(:case_track_value)] }
case_track_values { |a| [a.association(:case_track_value)] }
in CaseTrack. case_track_values { |a| [a.association(:case_track_value)] }
。
class CaseTrack
has_and_belongs_to_many CaseTrackValue
end
class CaseTrackValue
has_and_belongs_to_many CaseTrack
end
Rspec
it 'may exercise the create action' do
post '<route>', params: {case_track: attributes_for(:case_track)}
end
end
class CaseTrackController < ApplicationController
private:
def case_track_params
params.require(:case_track).permit(:name, :active, {case_track_values:[]})
end
end
Take a look at HABTM factories working in one of my projects. 看看在我的一个项目中工作的HABTM工厂。 I put here the whole setup within a common example for you could understand it deeper and other stackoverflowers could easily adapt that example for their use cases. 我把整个设置放在一个常见的例子中,你可以更深入地理解它,而其他stackoverflowers可以很容易地将这个例子用于他们的用例。
So we have books and categories. 所以我们有书籍和类别。 There could be book belonging to many categories and there could be category with many books in it. 可能存在属于许多类别的书籍,并且可能存在包含许多书籍的类别。
models/book.rb 车型/ book.rb
class Book < ActiveRecord::Base
has_and_belongs_to_many :categories
end
models/category.rb 车型/ category.rb
class Category < ActiveRecord::Base
has_and_belongs_to_many :books
end
factories/books.rb 工厂/ books.rb
FactoryGirl.define do
factory :book do
# factory to create one book with 1-3 categories book belongs to
factory :book_with_categories do
transient do
ary { array_of(Book) }
end
after(:create) do |book, ev|
create_list(:category, rand(1..3), books: ev.ary.push(book).uniq)
end
end
#factory to create a book and one category book belongs to
factory :book_of_category do
after(:create) do |book, ev|
create(:category, books: [book])
end
end
end
end
factories/categories.rb 工厂/ categories.rb
FactoryGirl.define do
factory :category do
#factory to create category with 3-10 books belong to it
factory :category_with_books do
transient do
ary { array_of(Category) }
num { rand(3..10) }
end
after(:create) do |cat, ev|
create_list(:book, ev.num,
categories: ev.ary.push(cat).uniq)
end
end
end
end
My helper method which you have to put somewhere in spec/support
and then include it where you need it: 我的帮助方法 ,您必须将其放在spec/support
某个位置,然后将其包含在您需要的位置:
# method returns 0-3 random objects of some Class (obj)
# for example 0-3 categories for you could add them as association
# to certain book
def array_of(obj)
ary = []
if obj.count > 0
Random.rand(0..3).times do
item = obj.all.sample
ary.push(item) unless ary.include?(item)
end
end
ary
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.