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.
You can reuse what you know from ActiveRecord.
has_manyandbelongs_towork just fine in Mongoid.But you can take advantage of document databases (which MongoDB is) and use some embedding. For
embedded_into work correctly you have to haveembeds_oneorembeds_manyon the other end. See http://mongoid.org/docs/relations/embedded/1-1.html