0

I have a Rails 3.1 app and I use DataMapper as ORM.

I have following models (stripped):

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

I found out that DataMapper retreives data in a different way when app is run as a server and console when calling Task#project.

When run as server it tries to retrieve a Group record, result is like this (see 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>

When run as console, and retreived manually via Group.get(4), result is like this (see 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>

Methods are not monkey patched:

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] 

The environment is the same in both scenarios.

SQL queries are not the same in both scenarios but both ask about value of project_id:

Console:

 ~   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

Server:

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

I've found out a workaround but it should not be required. If I change Task#project definition to:

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

it successfully retreives associated Task.

Any ideas what can cause such difference?

mspanc
  • 550
  • 5
  • 15

1 Answers1

0

Check these:

  1. Are you using the same environment when running the app on console and server.

  2. Turn the debug mode on DataMapper::Logger.new($stdout, :debug) and see what is the actual query being executed.

If you still have a problem, update your question with the output of the debug mode and comment to this answer

draxxxeus
  • 1,503
  • 1
  • 11
  • 14