{"id":387,"date":"2022-08-30T15:00:30","date_gmt":"2022-08-30T15:00:30","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/issue-with-mongoid-and-rails-when-do-seed-its-faild-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:00:30","modified_gmt":"2022-08-30T15:00:30","slug":"issue-with-mongoid-and-rails-when-do-seed-its-faild-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/issue-with-mongoid-and-rails-when-do-seed-its-faild-collection-of-common-programming-errors\/","title":{"rendered":"issue with mongoid and rails when do seed its faild-Collection of common programming errors"},"content":{"rendered":"<p>first is the status:<\/p>\n<pre><code>class Status\n  include Mongoid::Document\n  field :name, type: String\n  has_one :Apis\n  validates_presence_of :name\nend\n<\/code><\/pre>\n<p>as well have APIS:<\/p>\n<pre><code>class Apis\n  include Mongoid::Document\n  field :name, type: String\n  field :description, type: String, default: ''\n  field :create_at, type: DateTime, default: -&gt;{ DateTime.now }\n\n  belongs_to :Status\n  validates_presence_of :name\nend\n<\/code><\/pre>\n<p>the SEED file:<\/p>\n<pre><code># This file should contain all the record creation needed to seed the database with its default values.\n# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).\n#\n# Examples:\n#\n#   cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])\n#   Mayor.create(name: 'Emanuel', city: cities.first)\nif Status.count == 0 \n    Status.create({name: \"active\"})\n    Status.create({name: \"inactive\"})\n    Status.create({name: \"pending\"})\n    Status.create({name: \"rejected\"})\n    Status.create({name: \"sending\"})\n    Status.create({name: \"contentItemPending\"})\n    Status.create({name: \"contentItemListed\"})\n    Status.create({name: \"staticItemListed\"})\n    Status.create({name: \"requestItemPending\"})\nend\n\nif Apis.count == 0 \n    active_status = Status.find_by({name:\"active\"})\n    youtub = Apis.new({name:\"youtube\", description:\"Youtube API Used youtube_it gem\"})\n    youtub.build_Status(active_status.id)\n    youtub.save!\n    itunes = Apis.new({name:\"itunes\", description:\"ITUNES API Used itunes gem\"})\n    itunes.build_Status(active_status.id)\n    itunes.save!\n    factual = Apis.new({name:\"factual\", description:\"FACTUAL API Used FACTUAL gem\"})\n    factual.build_Status(active_status.id)\n    factual.save!\nend\n<\/code><\/pre>\n<p>now i get this error:<\/p>\n<pre><code>fastwings:Feelike-Agent\/ (master\u2717) $ rake db:seed                                                                                   [14:21:41]\nrake aborted!\nuninitialized constant Statu\n\/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'\n\/usr\/local\/rvm\/gems\/ruby-1.9.3-p327@global\/gems\/activesupport-3.2.9\/lib\/active_support\/inflector\/methods.rb:229:in `each'\n\/usr\/local\/rvm\/gems\/ruby-1.9.3-p327@global\/gems\/activesupport-3.2.9\/lib\/active_support\/inflector\/methods.rb:229:in `constantize'\n\/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'\n\/usr\/local\/rvm\/gems\/ruby-1.9.3-p327\/gems\/mongoid-3.0.19\/lib\/mongoid\/relations\/metadata.rb:602:in `klass'\n\/usr\/local\/rvm\/gems\/ruby-1.9.3-p327\/gems\/mongoid-3.0.19\/lib\/mongoid\/relations\/builders.rb:68:in `block in builder'\n\/home\/fastwings\/Projects\/Ruby\/Feelike-Agent\/db\/seeds.rb:23:in `'\n\/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 '\n\/usr\/local\/rvm\/gems\/ruby-1.9.3-p327\/bin\/ruby_noexec_wrapper:14:in `eval'\n\/usr\/local\/rvm\/gems\/ruby-1.9.3-p327\/bin\/ruby_noexec_wrapper:14:in `'\nTasks: TOP =&gt; db:seed\n(See full trace by running task with --trace)\n<\/code><\/pre>\n<p>now i got no clue what i doing here wrong what is here not work???<\/p>\n<p>as well its make at line 23 means youtub.build_Status(active_status.id) to uninitialized constant Statu why???<\/p>\n<p>ok Guys its work my error was this: belongs_to :Status , class_name: &#8216;something&#8217; and has_one :Apis ,class_name:&#8217;something&#8217; and the seeder need added like this:<\/p>\n<pre><code>Mongoid.purge!\nif SystemStatus.count == 0 \n    SystemStatus.create!({name: \"active\"})\n    SystemStatus.create!({name: \"inactive\"})\n    SystemStatus.create!({name: \"pending\"})\n    SystemStatus.create!({name: \"rejected\"})\n    SystemStatus.create!({name: \"sending\"})\n    SystemStatus.create!({name: \"contentItemPending\"})\n    SystemStatus.create!({name: \"contentItemListed\"})\n    SystemStatus.create!({name: \"staticItemListed\"})\n    SystemStatus.create!({name: \"requestItemPending\"})\nend\n\nif Providers.count == 0 \n    active_status = SystemStatus.find_by({name:\"active\"})\n    Providers.create!({name:\"youtube\", description:\"Youtube API Used youtube_it gem\",status: active_status})\n    Providers.create!({name:\"itunes\", description:\"Youtube API Used youtube_it gem\",status: active_status})\n    Providers.create!({name:\"youtube\", description:\"Youtube API Used youtube_it gem\",status: active_status})\n    Providers.create!({name:\"unknown\", description:\"Rest service conntection\",status: active_status})\nend\n<\/code><\/pre>\n<p>what that went wrong here was that it didnt know what class to go to as well how i enter the data<\/p>\n<ol>\n<li>\n<p>Besides the s missing, which that&#8217;s what it looks like, I&#8217;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&#8217;s an error. IF you use it without the bang, it&#8217;ll return false but you won&#8217;t know there&#8217;s a problem.<\/p>\n<\/li>\n<li>\n<p>You may have to set custom inflectors for your Status model. Fire up a console and try running <code>\"status\".singularize<\/code> to see what you get.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 18:41:30. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>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: &#8221; field :create_at, type: DateTime, default: -&gt;{ DateTime.now } belongs_to :Status validates_presence_of :name end the SEED file: # This file should [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-387","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/387","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=387"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/387\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=387"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=387"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=387"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}