質問

I have three models, Course, Category and partner, a course can have many categories and a course belongs to one partner. When i create my course factory i get the following error:

Partner has a valid factory for course
     Failure/Error: expect(FactoryGirl.create(:course)).to be_valid
     ActiveRecord::RecordInvalid:
       Validation failed: Name has already been taken 

Here are my models:

class Category < ActiveRecord::Base
  has_many :categorisations 
  has_many :courses, :through=> :categorisations
  belongs_to :user
#validation 
  validates :name, presence: true , uniqueness: { scope: :name }
end


class Partner < ActiveRecord::Base
  has_many :courses
  belongs_to :user

  validates :name, presence: true, uniqueness: { scope: :name }
  validates :short_name, presence: true
  VALID_HEX_COLOR= /\A#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})\z/
  validates :primary_color, presence: true, format: { with: VALID_HEX_COLOR}
  validates :secondary_color, presence: true, format: { with: VALID_HEX_COLOR}
end

class Course < ActiveRecord::Base
  extend FriendlyId
  friendly_id :title, use: [:slugged, :history]

  has_many :categorisations, :dependent => :destroy
  has_many :categories, :through=> :categorisations 
  belongs_to :partner
  belongs_to :user

  # validates_uniqueness_of :title
  validates :title, presence: true
  # validates :start_date, presence: true
  # validates :duration, presence:true
  # validates :state, presence:true
  validates :categories, length: { minimum: 1 , message:"please select"}
  validates :partner_id, presence: true, allow_nil: false
end

Here are my factories:

 factory :partner do |f|
    f.name Faker::Name.name
    f.short_name "UCT"
    f.primary_color "#009bda"
    f.secondary_color "#002060"
  end

factory :course do |f|
    f.title "Introduction to Accounting short course"
    f.start_date "2014-02-27 00:00:00"
    f.duration "10 WEEKS ONLINE"
    partner
    categorisation
  end  
  factory :categorisation do |categorisation|
    categorisation.category {|category| category.association(:category)}
    categorisation.course {|course| course.association(:course)}
  end

I am not to sure what i am doing wrong, if anyone could advise me on what the problem may be or the process i can go about fixing this problem may be that would be a great help

役に立ちましたか?

解決 2

All that i had to do was to add the following line to my course factory:

categories {[FactoryGirl.create(:category)]}

couse factory:

factory :course do |f|
    f.title "Introduction to Accounting short course"
    f.start_date "2014-02-27 00:00:00"
    f.duration "10 WEEKS ONLINE"
    partner
    categories {[FactoryGirl.create(:category)]}
  end

他のヒント

try this out:

 factory :partner do |f| 
    f.sequence(:name) { |n| "#{Faker::Name.name} #{n}" }
    f.short_name "UCT"
    f.primary_color "#009bda"
    f.secondary_color "#002060"
  end

  factory :category do |f| 
    f.sequence(:name) { |n| "Category #{n}" }
  end
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top