我有一个 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
我发现当应用程序作为服务器和控制台运行时,DataMapper 以不同的方式检索数据Task#project
。
当作为服务器运行时,它会尝试检索组记录,结果如下(参见 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
它成功地检索了关联的任务。
有什么想法会导致这种差异吗?