different result from scoped query with ActiveRecord #380

Closed
yosiyuki opened this Issue Jan 19, 2012 · 3 comments

Comments

Projects
None yet
3 participants
@yosiyuki

by the following scope notation, ActiveRecord and MongoMapper returns different result in production environments.

class Article < xxxx(MM or AR)
  scope :recent, where("created_at > ?", Time.now)
  scope :recent_by_block, lambda { where("created_at > ?", Time.now) }
end

With ActiveRecord,

  • scope#recent returns articles created after web server restarted (precisely, the first time scope#recent is called).
  • scope#recent_by_block returns no articles (since no record will be created after "now")

With MongoMapper,

  • both of scopes returns articles created after web server restarted (same as scope#recent in AR)

Though I'm not sure if this behavior is required in both of ORMs, my opinion is MM should follow the behavior of AR.

@jnunemaker

This comment has been minimized.

Show comment Hide comment
@jnunemaker

jnunemaker Jan 19, 2012

Contributor

Pretty sure that MM behaves the same. The difference is not at the ORM level but in ruby. The contents of a block are evaluated at call time, whereas the where with the time is evaluated when the code is loaded (boot time). Can you provide a script or example demonstrating this?

Contributor

jnunemaker commented Jan 19, 2012

Pretty sure that MM behaves the same. The difference is not at the ORM level but in ruby. The contents of a block are evaluated at call time, whereas the where with the time is evaluated when the code is loaded (boot time). Can you provide a script or example demonstrating this?

@yosiyuki

This comment has been minimized.

Show comment Hide comment
@yosiyuki

yosiyuki Jan 20, 2012

Here's the way to reproduce. It's definitely different at the ORM level.

Code

Model with timestamps

class Article < xxxx(MM or AR)
  scope :recent, where("created_at > ?", Time.now)
  scope :recent_by_block, lambda { where("created_at > ?", Time.now) }
end

Controller

class ArticleController < ApplicationController
  def recent
    @recent_articles = Article.recent.all
    @recent_articles_block = Article.recent_by_block.all
  end
end

Scenario

  1. Start web server of production env at 2012-01-19 01:00:00
  2. Check /articles/recent and you will see no record
  3. Create a record of article
  4. Check /articles/recent again and you'll see a record at MM case, besides AR returns no record.

Here's the way to reproduce. It's definitely different at the ORM level.

Code

Model with timestamps

class Article < xxxx(MM or AR)
  scope :recent, where("created_at > ?", Time.now)
  scope :recent_by_block, lambda { where("created_at > ?", Time.now) }
end

Controller

class ArticleController < ApplicationController
  def recent
    @recent_articles = Article.recent.all
    @recent_articles_block = Article.recent_by_block.all
  end
end

Scenario

  1. Start web server of production env at 2012-01-19 01:00:00
  2. Check /articles/recent and you will see no record
  3. Create a record of article
  4. Check /articles/recent again and you'll see a record at MM case, besides AR returns no record.
@cheald

This comment has been minimized.

Show comment Hide comment
@cheald

cheald Jul 7, 2013

Owner

Where with a SQL-style string is invalid in MM; the provided examples won't query as expected in MM. I can't reproduce this - please re-open with a MongoMapper-specific test case if you can still reproduce it.

Owner

cheald commented Jul 7, 2013

Where with a SQL-style string is invalid in MM; the provided examples won't query as expected in MM. I can't reproduce this - please re-open with a MongoMapper-specific test case if you can still reproduce it.

@cheald cheald closed this Jul 7, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment