I'm using a sequence in Factory Girl to get unique values but I'm getting validation errors

Posted by Sean Seefried on Stack Overflow See other posts from Stack Overflow or by Sean Seefried
Published on 2010-03-18T23:58:09Z Indexed on 2010/03/19 0:01 UTC
Read the original article Hit count: 403

Filed under:
|
|

I have a model defined this way

class Lga < ActiveRecord::Base
  validates_uniqueness_of :code
  validates_presence_of :name
end 

I've defined a factory for Lgas with

Factory.sequence(:lga_id) { |n| n + 10000 }

Factory.define :lga do |l|
  id = Factory.next :lga_id
  l.code "lga_#{id}"
  l.name "LGA #{id}"
end

However, when I run

Factory.create(:lga)
Factory.create(:lga)

in script/console I get

>> Factory.create(:lga)
=> #<Lga id: 2, code: "lga_10001", name: "LGA 10001", created_at: "2010-03-18  23:55:29", updated_at: "2010-03-18 23:55:29">
>> Factory.create(:lga)
ActiveRecord::RecordInvalid: Validation failed: Code has already been taken

© Stack Overflow or respective owner

Related posts about ruby

Related posts about rails