Ruby on rails mongoid和rails的问题何时种子失败

Ruby on rails mongoid和rails的问题何时种子失败,ruby-on-rails,ruby-on-rails-3,ruby-on-rails-3.2,mongoid,Ruby On Rails,Ruby On Rails 3,Ruby On Rails 3.2,Mongoid,首先是现状: class Status include Mongoid::Document field :name, type: String has_one :Apis validates_presence_of :name end 我们也有API: class Apis include Mongoid::Document field :name, type: String field :description, type: String, default: ''

首先是现状:

class Status
  include Mongoid::Document
  field :name, type: String
  has_one :Apis
  validates_presence_of :name
end
我们也有API:

class Apis
  include Mongoid::Document
  field :name, type: String
  field :description, type: String, default: ''
  field :create_at, type: DateTime, default: ->{ DateTime.now }

  belongs_to :Status
  validates_presence_of :name
end
种子文件:

# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
#
# Examples:
#
#   cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
#   Mayor.create(name: 'Emanuel', city: cities.first)
if Status.count == 0 
    Status.create({name: "active"})
    Status.create({name: "inactive"})
    Status.create({name: "pending"})
    Status.create({name: "rejected"})
    Status.create({name: "sending"})
    Status.create({name: "contentItemPending"})
    Status.create({name: "contentItemListed"})
    Status.create({name: "staticItemListed"})
    Status.create({name: "requestItemPending"})
end

if Apis.count == 0 
    active_status = Status.find_by({name:"active"})
    youtub = Apis.new({name:"youtube", description:"Youtube API Used youtube_it gem"})
    youtub.build_Status(active_status.id)
    youtub.save!
    itunes = Apis.new({name:"itunes", description:"ITUNES API Used itunes gem"})
    itunes.build_Status(active_status.id)
    itunes.save!
    factual = Apis.new({name:"factual", description:"FACTUAL API Used FACTUAL gem"})
    factual.build_Status(active_status.id)
    factual.save!
end
现在我得到了这个错误:

fastwings:Feelike-Agent/ (master✗) $ rake db:seed                                                                                   [14:21:41]
rake aborted!
uninitialized constant Statu
/usr/local/rvm/gems/ruby-1.9.3-p327@global/gems/activesupport-3.2.9/lib/active_support/inflector/methods.rb:230:in `block in constantize'
/usr/local/rvm/gems/ruby-1.9.3-p327@global/gems/activesupport-3.2.9/lib/active_support/inflector/methods.rb:229:in `each'
/usr/local/rvm/gems/ruby-1.9.3-p327@global/gems/activesupport-3.2.9/lib/active_support/inflector/methods.rb:229:in `constantize'
/usr/local/rvm/gems/ruby-1.9.3-p327@global/gems/activesupport-3.2.9/lib/active_support/core_ext/string/inflections.rb:54:in `constantize'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/mongoid-3.0.19/lib/mongoid/relations/metadata.rb:602:in `klass'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/mongoid-3.0.19/lib/mongoid/relations/builders.rb:68:in `block in builder'
/home/fastwings/Projects/Ruby/Feelike-Agent/db/seeds.rb:23:in `<top (required)>'
/usr/local/rvm/gems/ruby-1.9.3-p327/gems/mongoid-3.0.19/lib/mongoid/railties/database.rake:13:in `block (2 levels) in <top (required)>'
/usr/local/rvm/gems/ruby-1.9.3-p327/bin/ruby_noexec_wrapper:14:in `eval'
/usr/local/rvm/gems/ruby-1.9.3-p327/bin/ruby_noexec_wrapper:14:in `<main>'
Tasks: TOP => db:seed
(See full trace by running task with --trace)

这里出现的问题是,它不知道要去哪个类,也不知道我如何输入数据,除了缺少的,这就是它的样子,我不确定Status.count是否会工作,我想你需要使用Status.all.count,其他的是你应该使用Status.create!砰的一声(!)。如果出现错误,它将引发异常。如果您在没有爆炸的情况下使用它,它将返回false,但您不会知道有问题。

您可能必须为您的状态模型设置自定义屈折符。启动控制台,试着运行
“status”。将
设置为singularize,看看你得到了什么。

它可以工作,我确实有数据,但我得到了你说的,现在添加这个
Mongoid.purge!
if SystemStatus.count == 0 
    SystemStatus.create!({name: "active"})
    SystemStatus.create!({name: "inactive"})
    SystemStatus.create!({name: "pending"})
    SystemStatus.create!({name: "rejected"})
    SystemStatus.create!({name: "sending"})
    SystemStatus.create!({name: "contentItemPending"})
    SystemStatus.create!({name: "contentItemListed"})
    SystemStatus.create!({name: "staticItemListed"})
    SystemStatus.create!({name: "requestItemPending"})
end

if Providers.count == 0 
    active_status = SystemStatus.find_by({name:"active"})
    Providers.create!({name:"youtube", description:"Youtube API Used youtube_it gem",status: active_status})
    Providers.create!({name:"itunes", description:"Youtube API Used youtube_it gem",status: active_status})
    Providers.create!({name:"youtube", description:"Youtube API Used youtube_it gem",status: active_status})
    Providers.create!({name:"unknown", description:"Rest service conntection",status: active_status})
end