当Rails应用程序作为控制台和服务器运行时,DataMapper的行为会有所不同

时间:2013-05-21 17:02:58

标签: ruby-on-rails datamapper

我有一个Rails 3.1应用程序,我使用DataMapper作为ORM。

我有以下模型(剥离):

class Task
  include DataMapper::Resource
  belongs_to :group

  def project
    group && group.project
  end
end

class Group
  include DataMapper::Resource
  belongs_to :project
  has n, :tasks
end

class Project
  include DataMapper::Resource
  has n, :groups
end

我发现在调用Task#project时,当app作为服务器和控制台运行时,DataMapper会以不同的方式检索数据。

当作为服务器运行时,它尝试检索组记录,结果是这样的(参见project_id):

#<Group @id=4 @created_at=Fri, 19 Apr 2013 06:13:25 +0000 @updated_at=Fri, 19 Apr 2013 06:13:25 +0000 @name="In Progress" @weight=<not loaded> @sync_updated_at=<not loaded> @sync_created_at=<not loaded> @capacity=<not loaded> @project_id=nil>

当作为控制台运行并通过Group.get(4)手动检索时,结果如下(参见project_id):

#<Group @id=4 @created_at=Fri, 19 Apr 2013 06:13:25 +0000 @updated_at=Fri, 19 Apr 2013 06:13:25 +0000 @name="In Progress" @weight=<not loaded> @sync_updated_at=<not loaded> @sync_created_at=<not loaded> @capacity=<not loaded> @project_id=1>

方法不是猴子补丁:

1.9.1 :060 > Group.instance_method(:project).source_location
 => ["/var/www/app/shared/bundle/ruby/1.9.1/gems/dm-core-1.2.0/lib/dm-core/model/relationship.rb", 333] 
1.9.1 :061 > Group.instance_method(:project_id).source_location
 => ["/var/www/app/shared/bundle/ruby/1.9.1/gems/dm-core-1.2.0/lib/dm-core/model/property.rb", 206] 
1.9.1 :062 > Group.instance_method(:project_id=).source_location
 => ["/var/www/app/shared/bundle/ruby/1.9.1/gems/dm-core-1.2.0/lib/dm-core/model/property.rb", 235] 
1.9.1 :063 > Group.instance_method(:project=).source_location
 => ["/var/www/app/shared/bundle/ruby/1.9.1/gems/dm-core-1.2.0/lib/dm-core/model/relationship.rb", 358] 

两种情况下的环境都是相同的。

SQL查询在两种情况下都不相同,但都询问了project_id的值:

控制台:

 ~   SQL (0.038ms)  SET sql_auto_is_null = 0
 ~   SQL (0.028ms)  SET SESSION sql_mode = 'ANSI,NO_BACKSLASH_ESCAPES,NO_DIR_IN_CREATE,NO_ENGINE_SUBSTITUTION,NO_UNSIGNED_SUBTRACTION,TRADITIONAL'
 ~   SQL (0.033ms)  SELECT `id`, `type`, `created_at`, `updated_at`, `name`, `project_id` FROM `groups` WHERE `id` = 3 LIMIT 1

服务器:

SELECT `id`, `created_at`, `updated_at`, `type`, `name`, `project_id` FROM `groups` WHERE `id` = 3 ORDER BY `id`

我发现了一种解决方法,但不应该要求它。如果我将Task#project定义更改为:

def project
  group && Task.get(group.project_id)
end

它成功撤消了相关的任务。

任何想法会导致这种差异吗?

1 个答案:

答案 0 :(得分:0)

检查这些:

  1. 在控制台和服务器上运行应用时,您是否使用相同的environment

  2. DataMapper::Logger.new($stdout, :debug)上打开调试模式,看看正在执行的实际查询是什么。

  3. 如果您仍有问题,请使用调试模式的输出更新您的问题,并对此答案发表评论