issue with mongoid and rails when do seed its faild-Collection of common programming errors
first is the status:
class Status
include Mongoid::Document
field :name, type: String
has_one :Apis
validates_presence_of :name
end
as well have 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
the SEED file:
# 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
now i get this error:
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 `'
/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 '
/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 `'
Tasks: TOP => db:seed
(See full trace by running task with --trace)
now i got no clue what i doing here wrong what is here not work???
as well its make at line 23 means youtub.build_Status(active_status.id) to uninitialized constant Statu why???
ok Guys its work my error was this: belongs_to :Status , class_name: ‘something’ and has_one :Apis ,class_name:’something’ and the seeder need added like this:
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
what that went wrong here was that it didnt know what class to go to as well how i enter the data
-
Besides the s missing, which that’s what it looks like, I’m not sure if the Status.count will even work, I think you need to use Status.all.count, and other thing is you should use Status.create! with a bang (!). It will raise an exception if if there’s an error. IF you use it without the bang, it’ll return false but you won’t know there’s a problem.
-
You may have to set custom inflectors for your Status model. Fire up a console and try running
"status".singularize
to see what you get.
Originally posted 2013-11-09 18:41:30.