【问题标题】:How to run method of inherited class in mongodb?如何在mongodb中运行继承类的方法?
【发布时间】:2019-04-03 08:23:26
【问题描述】:

例如,我在下面创建了继承自 BaseClass 的 ChildClass:

class BaseClass

end

第一个子类

class ChildClass1 < BaseClass

end

或者其他

class ChildClass2 < BaseClass

end

使用 BaseClass 时 它可以正常工作。

但是当使用 ChildClass 1 或 2 时, 我不能做 childclass1.count 或 childclass1.first 错误如下:(在保存为显示后使用子类时)

[2] pry(#<TodosController>)> @todo = "Todo::TodoM#{$xvars["user_id"].to_s}".constantize.new
=> #<Todo::TodoM5ca1157c51d2f56bc4d5e379 _id: 5ca45c5651d2f5988c2ef5fa, created_at: nil, updated_at: nil, user_id: nil, title: nil, completed: nil, due: nil, detail: nil, image: nil, member: nil, status: nil, _type: "Todo::TodoM5ca1157c51d2f56bc4d5e379">
[3] pry(#<TodosController>)> @todo = "Todo::TodoM#{$xvars["user_id"].to_s}".constantize.new(
[3] pry(#<TodosController>)*   title: $xvars["form_todo"]["title"],
[3] pry(#<TodosController>)* user_id: $xvars["user_id"])
=> #<Todo::TodoM5ca1157c51d2f56bc4d5e379 _id: 5ca45ca351d2f5988c2ef5fb, created_at: nil, updated_at: nil, user_id: BSON::ObjectId('5ca1157c51d2f56bc4d5e379'), title: "sdfsdf", completed: nil, due: nil, detail: nil, image: nil, member: nil, status: nil, _type: "Todo::TodoM5ca1157c51d2f56bc4d5e379">
[4] pry(#<TodosController>)> @todo.save!
MONGODB | localhost:27017 | todo2_development.insert | STARTED | {"insert"=>"todos", "ordered"=>true, "lsid"=>{"id"=><BSON::Binary:0x70237914801960 type=uuid data=0x0c897c8df79a4052...>}, "documents"=>[{"_id"=>BSON::ObjectId('5ca45ca351d2f5988c2ef5fb'), "_type"=>"Todo::TodoM5ca1157c51d2f56bc4d5e379", "title"=>"sdfs...
MONGODB | localhost:27017 | todo2_development.insert | SUCCEEDED | 0.006s
=> true
[5] pry(#<TodosController>)> @todo.first
NoMethodError: undefined method `first' for #<Todo::TodoM5ca1157c51d2f56bc4d5e379:0x00007fc31453c828>
from (pry):6:in `create'
[6] pry(#<TodosController>)> @todo
=> #<Todo::TodoM5ca1157c51d2f56bc4d5e379 _id: 5ca45ca351d2f5988c2ef5fb, created_at: 2019-04-03 07:11:48 UTC, updated_at: 2019-04-03 07:11:48 UTC, user_id: BSON::ObjectId('5ca1157c51d2f56bc4d5e379'), title: "sdfsdf", completed: nil, due: nil, detail: nil, image: nil, member: nil, status: nil, _type: "Todo::TodoM5ca1157c51d2f56bc4d5e379">
[7] pry(#<TodosController>)> @todo.save!
=> true
[8] pry(#<TodosController>)> @todo.count
NoMethodError: undefined method `count' for #<Todo::TodoM5ca1157c51d2f56bc4d5e379:0x00007fc31453c828>
from (pry):9:in `create'
[9] pry(#<TodosController>)> @todo.first
NoMethodError: undefined method `first' for #<Todo::TodoM5ca1157c51d2f56bc4d5e379:0x00007fc31453c828>
from (pry):10:in `create'
[10] pry(#<TodosController>)> @todo.first
NoMethodError: undefined method `first' for #<Todo::TodoM5ca1157c51d2f56bc4d5e379:0x00007fc31453c828>
from (pry):11:in `create'
[11] pry(#<TodosController>)> @todo.class
=> Todo::TodoM5ca1157c51d2f56bc4d5e379

我做错了什么? (是的,我看到来自“创建”的错误,但它保存了!如图所示)

{
  "_id": ObjectId("5ca45ca351d2f5988c2ef5fb"),
  "_type": "Todo::TodoM5ca1157c51d2f56bc4d5e379",
  "title": "sdfsdf",
  "user_id": ObjectId("5ca1157c51d2f56bc4d5e379"),
  "updated_at": new Date(1554275508224),
  "created_at": new Date(1554275508224)
}

【问题讨论】:

  • 您可能会考虑实际包含语言标签,并在提问时特别提及正在使用的驱动程序和版本。我想使用 mongoid?
  • 是的。 Rails 5.2.3、Ruby 2.5.0、Mongodb V3.6.2 Mac OSX
  • 那么,mongoid 文档对inheritance 有什么要说的呢?请阅读文档,然后(如果需要)提供实际完成的课程示例和a small listing in order to reproduce the problem。请也阅读该链接。

标签: ruby mongodb class methods


【解决方案1】:

我发现 ChildClass 会像 BaseClass 一样工作。一开始不工作的原因是因为user_id为nil。

另一个问题是 mongodb 在内部处理其子类的方式,就像它的 BaseClass 中的另一种类型的数据一样!即使我们在创建时放置了命名空间。令我惊讶的是,来自 rails 的 mongodb 响应命名空间是不同的,它创建了另一个易于理解的集合(表)(也是我首先要寻找的)而不是隐藏为另一个让我一开始感到困惑的 _type(字段)。所以我想在这里为新手注意。

而不是我在下面期望的另一个集合

【讨论】:

    猜你喜欢
    • 2020-02-14
    • 1970-01-01
    • 2012-02-24
    • 1970-01-01
    • 2021-11-30
    • 1970-01-01
    • 1970-01-01
    • 2011-03-03
    • 2012-10-23
    相关资源
    最近更新 更多