与 Mongoid 的关联

发布于 2024-12-24 00:54:32 字数 1269 浏览 2 评论 0原文

我有三个简单的模型 - CarDetailsDetails2Car 有很多 DetailsDetails 有很多 Details2

使用 MySQL 和 ActiveRecord,我将设置如下关联:

class Car
  has_many :details
end


class Detail
   belongs_to :car
end

在视图中,我有一个汽车列表,我有:

<% @cars.each do |car| -%>
  <%=car.details.count%> #uninitialized constant Details
<% end %>

EDIT2: 这是我在 cars 表中的内容:

db.cars.find()
{ "_id" : ObjectId("4efe69716f85ce447a000054"), "name" : "bmw", "descr" : "asasgasga as gas gas ", "updated_at" : "Sat Dec 31 2011 02:46:25 GMT+0100 (CET)", "created_at" : "Sat Dec 31 2011 02:46:25 GMT+0100 (CET)" }

details

db.details.find()
{ "_id" : ObjectId("4f01106d6f85ce6b850000b8"), "car_id" : ObjectId("4efe69716f85ce447a000054"), "name" : "20120102030325", "descr" : "dsg", "updated_at" : "Mon Jan 02 2012 03:03:25 GMT+0100 (CET)", "created_at" : "Mon Jan 02 2012 03:03:25 GMT+0100 (CET)" }

CarsController 中,我仅在此操作中拥有:

def index
  @cars = Car.all
end

这就是我所做的全部。

I have three simple models - Car, Details and Details2.
Car have lot of Details and Details have lot of Details2.

With MySQL and ActiveRecord I will set the associations like:

class Car
  has_many :details
end


class Detail
   belongs_to :car
end

In the view, where I have a list of Car, I have:

<% @cars.each do |car| -%>
  <%=car.details.count%> #uninitialized constant Details
<% end %>

EDIT2:
This is what I have in cars table:

db.cars.find()
{ "_id" : ObjectId("4efe69716f85ce447a000054"), "name" : "bmw", "descr" : "asasgasga as gas gas ", "updated_at" : "Sat Dec 31 2011 02:46:25 GMT+0100 (CET)", "created_at" : "Sat Dec 31 2011 02:46:25 GMT+0100 (CET)" }

and details:

db.details.find()
{ "_id" : ObjectId("4f01106d6f85ce6b850000b8"), "car_id" : ObjectId("4efe69716f85ce447a000054"), "name" : "20120102030325", "descr" : "dsg", "updated_at" : "Mon Jan 02 2012 03:03:25 GMT+0100 (CET)", "created_at" : "Mon Jan 02 2012 03:03:25 GMT+0100 (CET)" }

And in CarsController I have in this action only:

def index
  @cars = Car.all
end

that's all what I do.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遗忘曾经 2024-12-31 00:54:32

您可以重用从 ActiveRecord 中了解到的信息。 has_manybelongs_to 工作正常在 Mongoid 中很好

但是您可以利用文档数据库(MongoDB 就是)并使用一些嵌入。为了使 embedded_in 正常工作,您必须在另一端安装 embeds_oneembeds_many。请参阅http://mongoid.org/docs/relations/embedded/1-1。 html

  class Person
    include Mongoid::Document
    embeds_one :name
  end

  class Name
    include Mongoid::Document
    field :vorname, type: String
    field :nachname, type: String
    embedded_in :person
  end

You can reuse what you know from ActiveRecord. has_many and belongs_to work just fine in Mongoid.

But you can take advantage of document databases (which MongoDB is) and use some embedding. For embedded_in to work correctly you have to have embeds_one or embeds_many on the other end. See http://mongoid.org/docs/relations/embedded/1-1.html

  class Person
    include Mongoid::Document
    embeds_one :name
  end

  class Name
    include Mongoid::Document
    field :vorname, type: String
    field :nachname, type: String
    embedded_in :person
  end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文