播种模型不适用于模型中的验证用户
Seeding model is not working with validatating user in model
我的种子看起来像:
seeds.rb
seed_file = File.join(Rails.root, 'db', 'seed.yml')
config = YAML::load_file(seed_file)
Article.create(config["articles"])
User::HABTM_Articles.create(config["articles_users"])
seed.yml
articles:
- status: 0
title: First article
body: Some awesome text
articles_users:
- article_id: 1
user_id: 1
我做rake db:drop && rake db:create && rake db:migrate
.
然后 运行 rake db:seed --trace
得到输出:
** Invoke db:seed (first_time)
** Execute db:seed
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:abort_if_pending_migrations
我的Article
table是空的,但是Article::HABTM_Users
是种子。
我发现问题出在 Article
模型中:
article.rb
class Article < ActiveRecord::Base
has_and_belongs_to_many :users
validates :title, presence: true, uniqueness: true
validates :body, presence: true
validates :users, presence: true #OK when remove this line
enum status: [:publ, :priv]
end
删除 validates :users, presence: true
播种后效果很好。
如何使用该用户存在验证进行播种 运行?
问题是,在您创建文章时,它没有用户。我知道您的种子文件将用户列为 紧随其后的事情,但 ActiveRecord 对验证没有耐心!因此,您需要做的是在创建文章的同时创建 ArticleUsers,您可以通过以下几种方式完成:
将 article_users
定义推入您的 articles
定义:
articles:
- status: 0
title: First article
body: Some awesome text
user_ids: [ 1 ]
我做了一些测试,这应该确实有效:
irb(main):006:0> Thing.create(name: "thing1", user_ids: [ 1 ] ).users
User Load (3.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = LIMIT 1 [["id", 1]]
SQL (1.4ms) INSERT INTO "things" ("name", "created_at", "updated_at") VALUES (, , ) RETURNING "id" [["name", "thing1"], ["created_at", "2015-07-25 16:38:47.717614"], ["updated_at", "2015-07-25 16:38:47.717614"]]
SQL (0.5ms) INSERT INTO "things_users" ("user_id", "thing_id") VALUES (, ) RETURNING "id" [["user_id", 1], ["thing_id", 2]]
=> #<ActiveRecord::Associations::CollectionProxy [#<User id: 1, name: "Bob", age: nil, created_at: "2015-07-25 16:37:13", updated_at: "2015-07-25 16:37:13">]>
使用 factories, which allow you to express a way to generate test objects more elegantly, a more robust fixture solution,或者只是将您的种子文件变成知道如何正确构建种子的 Ruby 脚本。
我的种子看起来像:
seeds.rb
seed_file = File.join(Rails.root, 'db', 'seed.yml')
config = YAML::load_file(seed_file)
Article.create(config["articles"])
User::HABTM_Articles.create(config["articles_users"])
seed.yml
articles:
- status: 0
title: First article
body: Some awesome text
articles_users:
- article_id: 1
user_id: 1
我做rake db:drop && rake db:create && rake db:migrate
.
然后 运行 rake db:seed --trace
得到输出:
** Invoke db:seed (first_time)
** Execute db:seed
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:abort_if_pending_migrations
我的Article
table是空的,但是Article::HABTM_Users
是种子。
我发现问题出在 Article
模型中:
article.rb
class Article < ActiveRecord::Base
has_and_belongs_to_many :users
validates :title, presence: true, uniqueness: true
validates :body, presence: true
validates :users, presence: true #OK when remove this line
enum status: [:publ, :priv]
end
删除 validates :users, presence: true
播种后效果很好。
如何使用该用户存在验证进行播种 运行?
问题是,在您创建文章时,它没有用户。我知道您的种子文件将用户列为 紧随其后的事情,但 ActiveRecord 对验证没有耐心!因此,您需要做的是在创建文章的同时创建 ArticleUsers,您可以通过以下几种方式完成:
将
article_users
定义推入您的articles
定义:articles: - status: 0 title: First article body: Some awesome text user_ids: [ 1 ]
我做了一些测试,这应该确实有效:
irb(main):006:0> Thing.create(name: "thing1", user_ids: [ 1 ] ).users User Load (3.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = LIMIT 1 [["id", 1]] SQL (1.4ms) INSERT INTO "things" ("name", "created_at", "updated_at") VALUES (, , ) RETURNING "id" [["name", "thing1"], ["created_at", "2015-07-25 16:38:47.717614"], ["updated_at", "2015-07-25 16:38:47.717614"]] SQL (0.5ms) INSERT INTO "things_users" ("user_id", "thing_id") VALUES (, ) RETURNING "id" [["user_id", 1], ["thing_id", 2]] => #<ActiveRecord::Associations::CollectionProxy [#<User id: 1, name: "Bob", age: nil, created_at: "2015-07-25 16:37:13", updated_at: "2015-07-25 16:37:13">]>
使用 factories, which allow you to express a way to generate test objects more elegantly, a more robust fixture solution,或者只是将您的种子文件变成知道如何正确构建种子的 Ruby 脚本。