to_yaml and association_proxy

Hi,

just a question / issue I came across when calling to_yaml on an object which defines an association and called a method on this association before the to_yaml. E.g. assume the following classes:

class Partner < ActiveRecord::Base   has_many :users end

class User < ActiveRecord::Base   def some_function   end end

if you have an instance of partner and you call:

partner.users.some_function

followed by:

partner.to_yaml

it will load and dump all associated user objects since once you called partner.users.some_function the @users instance variable exists in partner. Now to_yaml_properties just iterates over all instance variables and calls them via: instance_variable_get( m ) which in this case returns the association_proxy which will cause it to load all users associated with partner. This causes some trouble when a partner has a lot of users.

To prevent this I think it may be a good idea to define to_yaml_properties on ActiveRecord::Base and reject instance variables which are association proxies. This could naively be archived by this:

  def to_yaml_properties     instance_variables.reject { |i| instance_variable_get (i).respond_to?(:proxy_owner) }.sort   end

Thoughts?

To prevent this I think it may be a good idea to define to_yaml_properties on ActiveRecord::Base and reject instance variables which are association proxies. This could naively be archived by this:

def to_yaml_properties instance_variables.reject { |i| instance_variable_get (i).respond_to?(:proxy_owner) }.sort end

My gut feeling is that given that we don't override yaml conversion at all, we shouldn't attempt to address this behaviour. Either we should add a nice method ala to_xml and to_json that accept :only and :except and friends, or we should leave this for the user to deal with. The only place where I'm using .to_yaml in an application is with my own implementation, and perhaps people are relying on the current behaviour to include associations? Seems unlikely but... who knows.