HABTM Association with FactoryGirl

扶醉桌前 提交于 2020-01-05 03:35:12

问题


Have looked through and tried most examples and still cannot create a working HABTM. No matter how the CaseTrack and CaseTrackValue factories are constructed, I'm not able to find the CaseTrackValue[] in CaseTrack. Shouldn't creating CaseTrack properly provide a CaseTrackValue param within CaseTrack.
BTW: the only working association for HABTM seems to be putting

case_track_values { |a| [a.association(:case_track_value)] } in CaseTrack.

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

回答1:


Take a look at HABTM factories working in one of my projects. 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.

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

class Book < ActiveRecord::Base
  has_and_belongs_to_many :categories
end

models/category.rb

class Category < ActiveRecord::Base
  has_and_belongs_to_many :books
end

factories/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

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:

 # 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


来源:https://stackoverflow.com/questions/41787522/habtm-association-with-factorygirl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!