Querying on a many/belongs_to association

244 views
Skip to first unread message

Dru

unread,
Sep 14, 2011, 11:25:56 AM9/14/11
to MongoMapper
Stumbled across this earlier and I'm unsure if it's the expected or
not. There seems to be a problem retrieving documents from one side of
a many/belong_to association. Take the following:

require 'mongo_mapper'

MongoMapper.connection = Mongo::Connection.new("127.0.0.1")
MongoMapper.database = "association_test"

class Author
include MongoMapper::Document

key :name
many :books
end

class Book
include MongoMapper::Document

key :title
belongs_to :author
end

a = Author.create(:name => "Bob")
b = a.books.create(:title => 'bobs life')

I would assume I could get all of the books for the author by doing

Author.first.books

However, that just returns an empty array. Querying from the other
side is fine however:

b.author

this gives me back the author the book belongs to.

Is this the case for anyone else? For now I'm working around it so
it's not a huge issue.

Thanks.

Jamie Orchard-Hays

unread,
Sep 14, 2011, 11:38:17 AM9/14/11
to mongo...@googlegroups.com
in Book:

key :author_id

> --
> You received this message because you are subscribed to the Google
> Groups "MongoMapper" group.
> For more options, visit this group at
> http://groups.google.com/group/mongomapper?hl=en?hl=en

Dru

unread,
Sep 14, 2011, 1:07:00 PM9/14/11
to MongoMapper
Just tried adding that and it's still returning an empty array.
Everything seems to be getting persisted correctly as all the relevant
keys are there:

#<Author _id: BSON::ObjectId('4e70c0b179473ba396000001'), name: "Bob">
#<Book _id: BSON::ObjectId('4e70c0b179473ba396000003'), author_id:
BSON::ObjectId('4e70c0b179473ba396000001'), title: "bobs life">

Any other ideas? I'm using the latest gem release of mongomapper and
ruby 1.9.2 if that's of any additional help (this also happens with
master from github too).

Jamie Orchard-Hays

unread,
Sep 14, 2011, 3:12:37 PM9/14/11
to mongo...@googlegroups.com
I can't imagine what the problem is. I'm on Ruby 1.8.7, Rails 2.3.14 and MM 0.8.6. This is such a commonly-used feature of MM, I can't imagine it's not working. Can you give any more details? Have you looked at the objects in a mongo shell to verify their proper storage?

Dru

unread,
Sep 14, 2011, 5:44:48 PM9/14/11
to MongoMapper
Just double checked everything is stored correctly through the mongo
shell and it all looks fine:

> db.authors.find()
{ "_id" : ObjectId("4e70c0b179473ba396000001"), "name" : "Bob" }
> db.books.find()
{ "_id" : ObjectId("4e70c0b179473ba396000003"), "title" : "bobs life",
"author_id" : ObjectId("4e70c0b179473ba396000001") }

I've tried an older version of mongo_mapper (0.8.6) and it resulted in
the same problem. I have this problem in production as well so it's
not something specific to my machine it seems.

There aren't really any other details to give as I'm able to reproduce
the problem just using the bare bones example in the first post.I'm on
a mac (my production system is a linux machine). I'm running the
current stable release of ruby 1.9.2. I'm happy to provide any other
details to help figure this out though.

Jon Kern

unread,
Sep 14, 2011, 7:51:37 PM9/14/11
to mongo...@googlegroups.com
Not that this helps you at all...

But, it works here using mmconsole

mongomapper 0.8.6, plucky 0.4.1


>> class Author
>>   include MongoMapper::Document
>>   key :name
>>   many :books
>> end
=> nil
>>
?> class Book

>>   include MongoMapper::Document
>>   key :title
>>   #key :author_id
?>   belongs_to :author
>> end
=> nil
>>
?> a = Author.create(:name => "Bob")
=> #<Author name: "Bob", _id: BSON::ObjectId('4e713cca8951a2e8e2000001')>

>> b = a.books.create(:title => 'bobs life')
=> #<Book title: "bobs life", _id: BSON::ObjectId('4e713cca8951a2e8e2000002'), author_id: BSON::ObjectId('4e713cca8951a2e8e2000001')>
>> Author.first.books
=> [#<Book title: "bobs life", _id: BSON::ObjectId('4e713c4f8951a2e8a7000002'), author_id: BSON::ObjectId('4e713c148951a2e8a7000001')>]
>> b.author
=> #<Author name: "Bob", _id: BSON::ObjectId('4e713cca8951a2e8e2000001')>

And with ruby 1.9.2, mongo_mapper 0.92

ruby-1.9.2-p180 :001 > class Author
ruby-1.9.2-p180 :002?>     include MongoMapper::Document
ruby-1.9.2-p180 :003?>     key :name
ruby-1.9.2-p180 :004?>     many :books
ruby-1.9.2-p180 :005?>   end
 => nil
ruby-1.9.2-p180 :006 >
ruby-1.9.2-p180 :007 >   class Book
ruby-1.9.2-p180 :008?>     include MongoMapper::Document
ruby-1.9.2-p180 :009?>     key :title
ruby-1.9.2-p180 :010?>     #key :author_id
ruby-1.9.2-p180 :011 >       belongs_to :author
ruby-1.9.2-p180 :012?>   end
 => nil
ruby-1.9.2-p180 :013 >
ruby-1.9.2-p180 :014 >   a = Author.create(:name => "Bob")
 => #<Author _id: BSON::ObjectId('4e713da18951a2ec1a000001'), name: "Bob">
ruby-1.9.2-p180 :015 > b = a.books.create(:title => 'bobs life')
 => #<Book _id: BSON::ObjectId('4e713da18951a2ec1a000003'), author_id: BSON::ObjectId('4e713da18951a2ec1a000001'), title: "bobs life">
ruby-1.9.2-p180 :016 > Author.first.books
 => [#<Book _id: BSON::ObjectId('4e713c4f8951a2e8a7000002'), author_id: BSON::ObjectId('4e713c148951a2e8a7000001'), title: "bobs life">]
ruby-1.9.2-p180 :017 > b.author
 => #<Author _id: BSON::ObjectId('4e713da18951a2ec1a000001'), name: "Bob">
ruby-1.9.2-p180 :018 >

jon

blog: http://technicaldebt.com
twitter: http://twitter.com/JonKernPA

Dru said the following on 9/14/11 11:25 AM:

Jon Kern

unread,
Sep 14, 2011, 7:53:25 PM9/14/11
to mongo...@googlegroups.com
major dittos...

it must be you, Dru <g>

jon


Jamie Orchard-Hays said the following on 9/14/11 3:12 PM:

Maxmiliano Franco Braga

unread,
Sep 14, 2011, 8:11:00 PM9/14/11
to mongo...@googlegroups.com
Have you tried to parse the result into an Array?

I don´t know why... but sometimes I had to do this.

Try 
Author.first.books.to_a
just to see what you get.

Maxmiliano

2011/9/14 Jon Kern <jonk...@gmail.com>

Dru

unread,
Sep 14, 2011, 8:25:28 PM9/14/11
to MongoMapper
Found the problem. I'm running mongodb 2.0 (wasn't paying attention
when I was updating packages earlier, I thought I was still in 1.8).
Quick rollback to 1.8.3 and everything is working fine.

On Sep 15, 1:11 am, Maxmiliano Franco Braga <maxmili...@gmail.com>
wrote:
> Have you tried to parse the result into an Array?
>
> I don´t know why... but sometimes I had to do this.
>
> Try
> Author.first.books.to_a
> just to see what you get.
>
> Maxmiliano
>
> 2011/9/14 Jon Kern <jonker...@gmail.com>
>
>
>
>
>
>
>
> > major dittos...
>
> > it must be you, Dru <g>
>
> > jon
>
> > blog:http://technicaldebt.com
> > twitter:http://twitter.com/JonKernPA
>
> > Jamie Orchard-Hays said the following on 9/14/11 3:12 PM:
>
> >  This is such a commonly-used feature of MM, I can't imagine it's not
> >> working.
>
> > --
> > You received this message because you are subscribed to the Google
> > Groups "MongoMapper" group.
> > For more options, visit this group at
> >http://groups.google.com/**group/mongomapper?hl=en?hl=en<http://groups.google.com/group/mongomapper?hl=en?hl=en>
Reply all
Reply to author
Forward
0 new messages