Bug when serializing to_xml when :foreign_key is named like association

Think of the following models:

class User < ActiveRecord::Base   has_many :created_entities, :class_name => "Entity", :foreign_key => "created_by" end

class Entity < ActiveRecord::Base   belongs_to :created_by, :class_name => "User", :foreign_key => "created_by" end

There is also a created_by integer field in the Entity migration. Everything seems to work fine with these model (fetching the association and so on), but when I try to serialize Entity.to_xml :include => :created_by (or include something other the model has as association) it fails (NoMethodError: undefined method `macro' for nil:NilClass). For full trace see: ruby on rails - NoMethodError when serializing to_xml - Stack Overflow

But as soon as I rename the association in the Entity model to: belongs_to :creator, :class_name => "User", :foreign_key => "created_by" the serialization to_xml start to work again.

Im using Rails 3.0.1. Can someone reproduce that?

Guess what ... solved in Rails 3.0.3 :slight_smile:

Guess what ... solved in Rails 3.0.3 :slight_smile:

I'd say you're skating on thin ice: what's some_entity.created_by supposed to return: the foreign_key or an instance of User?