2012-11-28 2 views
3

나는공장 소녀 다수의 중첩 된 속성은

내가 4 개 중첩 평가와 공장 심사를 만들 필요가
class Rating < ActiveRecord::Base 
    attr_accessible :type_id, :value 
    belongs_to :review 

class Review < ActiveRecord::Base 
    attr_accessible :name, :description, :user_id, :relationship_type_id, :ratings_attributes  
    belongs_to :facility 
    has_many :ratings 
    accepts_nested_attributes_for :ratings, limit: Rating::RATE.count 

는, 테스트 검토 확인을 위해 그것은`모델을 가지고,하지만 난 어떤 생각을 가지고 있겠지 내 공장 : 컨트롤러에서

factory :review do 
    facility 
    sequence(:name) { |n| "Name of review #{n}" } 
    sequence(:description) { |n| "asdasdasdasdasdasd #{n}" } 
    user_id 1 
    relationship_type_id 1 
    end 
    factory :rating do 
    type_id 2 
    value 3 
    review 
    factory :requred_rating do 
     type_id 1 
    end 
    end 

내가 중첩 된 attrs에와 초기화 검토를 위해 쓰고 있어요 :

@review = Review.new 
    Rating::RATE.each do |rate| 
    @review.ratings.build(type_id: rate[1]) 
    end 

및 평가와 새로운 검토를 만드는 :

@review = @facility.reviews.build(params[:review]) 
    if @review.save 

모든 작업이 좋은,하지만 난 그게

제발 도와주세요 테스트해야합니다.

+0

은 당신이 공유해야 내 문제 –

+3

의 해결책을 발견했다. .. – hellion

답변

1

는이 같은 평가와 리뷰를 검토 공장에 특성을 추가 한 다음 생성 할 수 FactoryGirl.create(:review, :with_ratings)

검토 공장을 특징으로 :

factory :review do 
    facility 
    sequence(:name) { |n| "Name of review #{n}" } 
    sequence(:description) { |n| "asdasdasdasdasdasd #{n}" } 
    user_id 1 
    relationship_type_id 1 

    trait :with_ratings do 
    after(:create) do |review| 
     Rating::RATE.each do |rate| 
     FactoryGirl.create(:rating, review: review, type_id: rate[1]) 
     end 
    end 
    end 
end 
관련 문제