Reload appears to be broke in 0.9.2 when run in a rails app but not a ruby app

18 views
Skip to first unread message

Scott Tamosunas

unread,
Sep 16, 2011, 11:03:56 AM9/16/11
to MongoMapper
I created a simple rails app (3.0.4) and ran the following code from
the console:

MongoMapper.database = 'scott_testing'

class ScottDoc
include MongoMapper::Document

key :name, String
end

#Create a doc with the name set to 'scott'
sdoc = ScottDoc.new
sdoc.name = "scott"
sdoc.save!

#Get another copy of the doc
another_sdoc = ScottDoc.find(sdoc.id)

#Set the original doc name to nil and save
sdoc.name = nil
sdoc.save!

#Reload the another doc to get the latest data from the DB
p another_sdoc.reload #expect another_sdoc.name to be nil, but it's
'scott' - INCORRECT

However, if I run the same code as just a straight ruby app like so,
it works.

require 'rubygems'
require 'mongo_mapper'
require 'pp'

MongoMapper.database = 'scott_testing'

class ScottDoc
include MongoMapper::Document

key :name, String

end

#Create a doc with the name set to 'scott'
sdoc = ScottDoc.new
sdoc.name = "scott"
sdoc.save!

#Get another copy of the doc
another_sdoc = ScottDoc.find(sdoc.id)

#Set the original doc name to nil and save
sdoc.name = nil
sdoc.save!

#Reload the another doc to get the latest data from the DB
p another_sdoc.reload #another_sdoc.scott is nil - CORRECT

Any ideas on what might be going on here?

Thanks!

Scott

Scott Tamosunas

unread,
Sep 16, 2011, 2:16:54 PM9/16/11
to MongoMapper
It looks like this behavior was introduced in 0.9.2, I don't see this
problem with 0.9.1.

Scott

Scott Tamosunas

unread,
Sep 16, 2011, 4:13:47 PM9/16/11
to MongoMapper
I haven't determined why yet, but it seems to be related to this
commit:

https://github.com/jnunemaker/mongomapper/commit/5957d7ec285ff657f05c633f47c5ef8d45902a6e

When I revert this, the problem goes away. Seems kinda odd this only
happens when in a Rails app.

Scott

Jamie Orchard-Hays

unread,
Sep 16, 2011, 4:48:26 PM9/16/11
to mongo...@googlegroups.com
Looking at that code...

The new code calls select() on the Hash, which returns an Array of Arrays (pairs). Then each_pair() is called on that.

However, each_pair is NOT a method on Array.

Looks like a bug. Is there a test covering this?

Jamie

> --
> 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

Brandon Keepers

unread,
Sep 21, 2011, 8:56:57 AM9/21/11
to mongo...@googlegroups.com
Thanks for uncovering this. None of the tests are failing. Want to see if you can produce a failing test and we'll get this fixed up?

=b

Scott Tamosunas

unread,
Sep 21, 2011, 9:45:57 AM9/21/11
to MongoMapper
Hey Brandon,

I just figured out why this isn't breaking any test, you fixed it. :-)
Probably not intentionally but here's the commit:

https://github.com/jnunemaker/mongomapper/commit/afecd0c58b746d64241fb0f44235391132ad28c8

Undo that commit and write a test like below and you can easily see
the failure. The reason it was failing was, the previous commit now
doesn't even write to the document if the attribute is null, where in
the past, it would write "name : null" into the doc.

So the previous commit I cited was the problem per-se, but it was a
bummer that a test didn't fail because of it. I can write some tests
to characterize this failure if you want and submit a pull request.

Any chance we can get a 0.9.3 with this fix?

Thanks!

context "#reload" do
should "work" do
doc = ScottDoc.new
doc.name = "scott"
doc.save!

another_doc = ScottDoc.find(doc.id)
another_doc.name.should == doc.name
doc.name = nil
doc.save!
another_doc.reload
another_doc.name.should be_nil
end
end


On Sep 21, 8:56 am, Brandon Keepers <bran...@opensoul.org> wrote:
>  Thanks for uncovering this. None of the tests are failing. Want to see if you can produce a failing test and we'll get this fixed up?
>
> =b
>
>
>
> On Friday, September 16, 2011 at 4:48 PM, Jamie Orchard-Hays wrote:
> > Looking at that code...
>
> > The new code calls select() on the Hash, which returns an Array of Arrays (pairs). Then each_pair() is called on that.
>
> > However, each_pair is NOT a method on Array.
>
> > Looks like a bug. Is there a test covering this?
>
> > Jamie
>
> > On Sep 16, 2011, at 4:13 PM, Scott Tamosunas wrote:
>
> > > I haven't determined why yet, but it seems to be related to this
> > > commit:
>
> > >https://github.com/jnunemaker/mongomapper/commit/5957d7ec285ff657f05c...
>
> > > When I revert this, the problem goes away. Seems kinda odd this only
> > > happens when in a Rails app.
>
> > > Scott
>
> > > On Sep 16, 2:16 pm, Scott Tamosunas <tamosu...@gmail.com (http://gmail.com)> wrote:
> > > > It looks like this behavior was introduced in 0.9.2, I don't see this
> > > > problem with 0.9.1.
>
> > > > Scott
>
> > > > On Sep 16, 11:03 am, Scott Tamosunas <tamosu...@gmail.com (http://gmail.com)> wrote:
>
> > > > > I created a simple rails app (3.0.4) and ran the following code from
> > > > > the console:
>
> > > > > MongoMapper.database = 'scott_testing'
>
> > > > > class ScottDoc
> > > > >  include MongoMapper::Document
>
> > > > >  key :name, String
> > > > > end
>
> > > > > #Create a doc with the name set to 'scott'
> > > > > sdoc = ScottDoc.new
> > > > > sdoc.name (http://sdoc.name) = "scott"
> > > > > sdoc.save!
>
> > > > > #Get another copy of the doc
> > > > > another_sdoc = ScottDoc.find(sdoc.id)
>
> > > > > #Set the original doc name to nil and save
> > > > > sdoc.name (http://sdoc.name) = nil
> > > > > sdoc.save!
>
> > > > > #Reload the another doc to get the latest data from the DB
> > > > > p another_sdoc.reload #expect another_sdoc.name (http://another_sdoc.name) to be nil, but it's
> > > > > 'scott' - INCORRECT
>
> > > > > However, if I run the same code as just a straight ruby app like so,
> > > > > it works.
>
> > > > > require 'rubygems'
> > > > > require 'mongo_mapper'
> > > > > require 'pp'
>
> > > > > MongoMapper.database = 'scott_testing'
>
> > > > > class ScottDoc
> > > > >  include MongoMapper::Document
>
> > > > >  key :name, String
>
> > > > > end
>
> > > > > #Create a doc with the name set to 'scott'
> > > > > sdoc = ScottDoc.new
> > > > > sdoc.name (http://sdoc.name) = "scott"
> > > > > sdoc.save!
>
> > > > > #Get another copy of the doc
> > > > > another_sdoc = ScottDoc.find(sdoc.id)
>
> > > > > #Set the original doc name to nil and save
> > > > > sdoc.name (http://sdoc.name) = nil

Brandon Keepers

unread,
Sep 21, 2011, 9:53:25 AM9/21/11
to mongo...@googlegroups.com
Scott,

Oh, good catch. A pull request with a test that would have failed would be great. I'll keep an eye out for it and push out 0.9.3 after it comes in.

Thanks!
=b

Scott Tamosunas

unread,
Sep 21, 2011, 1:48:12 PM9/21/11
to MongoMapper
Ok, I submitted a pull request for the following branch:

https://github.com/scotttam/mongomapper/tree/test_document_reload

Thanks,

Scott

On Sep 21, 9:53 am, Brandon Keepers <bran...@opensoul.org> wrote:
>  Scott,
>
> Oh, good catch. A pull request with a test that would have failed would be great. I'll keep an eye out for it and push out 0.9.3 after it comes in.
>
> Thanks!
> =b
>
>
>
> On Wednesday, September 21, 2011 at 9:45 AM, Scott Tamosunas wrote:
> > Hey Brandon,
>
> > I just figured out why this isn't breaking any test, you fixed it. :-)
> > Probably not intentionally but here's the commit:
>
> >https://github.com/jnunemaker/mongomapper/commit/afecd0c58b746d64241f...
>
> > Undo that commit and write a test like below and you can easily see
> > the failure. The reason it was failing was, the previous commit now
> > doesn't even write to the document if the attribute is null, where in
> > the past, it would write "name : null" into the doc.
>
> > So the previous commit I cited was the problem per-se, but it was a
> > bummer that a test didn't fail because of it. I can write some tests
> > to characterize this failure if you want and submit a pull request.
>
> > Any chance we can get a 0.9.3 with this fix?
>
> > Thanks!
>
> >  context "#reload" do
> >  should "work" do
> >  doc = ScottDoc.new
> > doc.name (http://doc.name) = "scott"
> >  doc.save!
>
> >  another_doc = ScottDoc.find(doc.id)
> >  another_doc.name.should == doc.name (http://doc.name)
> > doc.name (http://doc.name) = nil
> >  doc.save!
> >  another_doc.reload
> >  another_doc.name.should be_nil
> >  end
> >  end
>
> > > > > > > p another_sdoc.reload #expect another_sdoc.name (http://another_sdoc.name(http://_sdoc.name)) to be nil, but it's
Reply all
Reply to author
Forward
0 new messages