ruby on rails - Seeding fails: "field can't be blank" but I'm seeding with a value -


i have required field article table:

t.string  :article_type, null: false 

and in model:

validates :article_type,  presence: true enum article_type:        [ :type1, :type2, :type3, :type4, :type5 ] 

in seeds have:

books = book.all books.each |book|   title = faker::lorem.sentence(3)   article_type = ["type1", "type2", "type3", "type4", "type5"].sample   book.articles.create!( title: title,                          article_type: article_type ) end 

problem: create line produces error: activerecord::recordinvalid: validation failed: article type can't blank. causing this? (i can confirm .sample line works , picks 1 of 5 types)

update: if change article_type string integer, works. should do? because isn't integer, it...?

rails enum expects corresponding db column integer, yours string.

so either change integer or substitute enum value validation.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -