{"action":"created","issue":{"url":"https://api.github.com/repos/thiagopradi/octopus/issues/347","labels_url":"https://api.github.com/repos/thiagopradi/octopus/issues/347/labels{/name}","comments_url":"https://api.github.com/repos/thiagopradi/octopus/issues/347/comments","events_url":"https://api.github.com/repos/thiagopradi/octopus/issues/347/events","html_url":"https://github.com/thiagopradi/octopus/issues/347","id":128486279,"number":347,"title":"ar-octopus (0.8.5), rails (4.2.4), Current shard is reseted in block when  'each' or 'map' methods are triggered on ActiveRecord::Associations::CollectionProxy objects.","user":{"login":"biscar","id":5123005,"avatar_url":"https://avatars.githubusercontent.com/u/5123005?v=3","gravatar_id":"","url":"https://api.github.com/users/biscar","html_url":"https://github.com/biscar","followers_url":"https://api.github.com/users/biscar/followers","following_url":"https://api.github.com/users/biscar/following{/other_user}","gists_url":"https://api.github.com/users/biscar/gists{/gist_id}","starred_url":"https://api.github.com/users/biscar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/biscar/subscriptions","organizations_url":"https://api.github.com/users/biscar/orgs","repos_url":"https://api.github.com/users/biscar/repos","events_url":"https://api.github.com/users/biscar/events{/privacy}","received_events_url":"https://api.github.com/users/biscar/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2016-01-25T08:27:15Z","updated_at":"2016-01-25T12:20:23Z","closed_at":"2016-01-25T12:20:23Z","body":"Current shard is reseted in block when  'each' or 'map' methods are triggered on ActiveRecord::Associations::CollectionProxy objects.\r\n\r\nI have master and main_1 shards.\r\n```\r\nclass Customer < ActiveRecord::Base\r\n  has_many :orders\r\nend\r\n```\r\nI think it is incorrect behavior:\r\n```\r\ncustomer = Customer.first; #master shard\r\nputs Octopus.using(:main_1) { customer.orders.map { |op| ActiveRecord::Base.connection.current_shard } }\r\n[:master, :master, :master] - #should be [:main_1, :main_1, :main_1]\r\n\r\nOctopus.using(:main_1) do\r\ncustomer = Customer.first\r\n puts Octopus.using(:master) { customer.orders.map { |op| ActiveRecord::Base.connection.current_shard } }\r\nend\r\n[:main_1, :main_1, :main_1] #should be [:master, :master, :master]\r\n```\r\nRails 3: customer.orders returns a Array.\r\nRails 4: customer.orders returns a 'ActiveRecord::Associations::CollectionProxy' object\r\n\r\nMethods map, each etc. are not available for 'ActiveRecord::Associations::CollectionProxy' object.\r\n\r\nAs result 'method_missing'(octopus/relation_proxy.rb) method is triggered and Octopus resets current shard and sets it in shard of owner(customer object) (octopus/collection_proxy.rb).\r\n\r\nI think it is correct behavior:\r\n```\r\ncustomer = Customer.first; #master shard\r\nputs Octopus.using(:main_1) { customer.orders.to_a.map { |op| ActiveRecord::Base.connection.current_shard } }\r\n[:main_1, :main_1, :main_1] - is correct behavior\r\n\r\nputs Octopus.using(:main_1) { Customer.first.orders.map { |op| ActiveRecord::Base.connection.current_shard } }\r\n[:main_1, :main_1, :main_1] - is correct behavior\r\n```\r\n\r\nWe see a different behavior in Rails 3:\r\n```\r\ncustomer = Customer.first; #master shard\r\nputs Octopus.using(:main_1) { customer.orders.map { |op| ActiveRecord::Base.connection.current_shard } }\r\n [:main_1, :main_1, :main_1]\r\n```\r\nI think shoud be same behavior in Rails 3 and Rails 4.\r\nCould you fix this, please?\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/thiagopradi/octopus/issues/comments/174488778","html_url":"https://github.com/thiagopradi/octopus/issues/347#issuecomment-174488778","issue_url":"https://api.github.com/repos/thiagopradi/octopus/issues/347","id":174488778,"user":{"login":"biscar","id":5123005,"avatar_url":"https://avatars.githubusercontent.com/u/5123005?v=3","gravatar_id":"","url":"https://api.github.com/users/biscar","html_url":"https://github.com/biscar","followers_url":"https://api.github.com/users/biscar/followers","following_url":"https://api.github.com/users/biscar/following{/other_user}","gists_url":"https://api.github.com/users/biscar/gists{/gist_id}","starred_url":"https://api.github.com/users/biscar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/biscar/subscriptions","organizations_url":"https://api.github.com/users/biscar/orgs","repos_url":"https://api.github.com/users/biscar/repos","events_url":"https://api.github.com/users/biscar/events{/privacy}","received_events_url":"https://api.github.com/users/biscar/received_events","type":"User","site_admin":false},"created_at":"2016-01-25T12:20:23Z","updated_at":"2016-01-25T12:20:23Z","body":"Might you check my fix?\r\nhttps://github.com/biscar/octopus/commit/44ec699708e4d793f86471ff84c6d2dd27047a9d"}}