0

首先是状态:

class Status
  include Mongoid::Document
  field :name, type: String
  has_one :Apis
  validates_presence_of :name
end

还有APIS:

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)

现在我不知道我在这里做错了什么在这里不起作用???

以及它在第 23 行的 make 意味着 youtube.build_Status(active_status.id) 到未初始化的常量Statu 为什么???

好的,伙计们,我的错误是:belongs_to :Status , class_name: 'something' 和 has_one :Apis ,class_name:'something' 并且播种机需要像这样添加:

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

这里出了什么问题,它不知道我要上什么课以及我如何输入数据

4

2 回答 2

1

除了 s 不见了,这就是它的样子,我不确定 Status.count 是否会工作,我认为你需要使用 Status.all.count,另外你应该使用 Status.create!一声巨响(!)。如果有错误,它将引发异常。如果你在没有爆炸的情况下使用它,它会返回 false 但你不会知道有问题。

于 2013-01-29T13:04:52.033 回答
0

您可能必须为您的状态模型设置自定义变形器。启动控制台并尝试运行"status".singularize以查看您得到的结果。

于 2013-05-03T02:05:57.610 回答