I’ve updated my server to ruby 1.9.2 and this stopped working (rails 3.0.6):
def index
@musicians = Musician.includes(:instruments)
render :xml => @musicians.to_xml( :include => :instruments )
end
And the models:
class Musician < ActiveRecord::Base
has_and_belongs_to_many :instruments
end
class Instrument < ActiveRecord::Base
has_and_belongs_to_many :musicians
end
I’m getting this error:
undefined method `type' for nil:NilClass
Framework trace:
activesupport (3.0.6) lib/active_support/whiny_nil.rb:48:in `method_missing'
activerecord (3.0.6) lib/active_record/serializers/xml_serializer.rb:230:in `compute_type'
activemodel (3.0.6) lib/active_model/serializers/xml.rb:22:in `initialize'
activemodel (3.0.6) lib/active_model/serializers/xml.rb:75:in `new'
activemodel (3.0.6) lib/active_model/serializers/xml.rb:75:in `block in serializable_attributes'
Any clue what I’m doing wrong?
Maybe this is related to: https://rails.lighthouseapp.com/projects/8994/tickets/4840-to_xml-doesnt-work-in-such-case-eventselecttitle-as-tto_xml
This is a core issue with Rails. What’s happening is that when Instruments are being included, an
instrument_idattribute is getting added. Then, when each Instrument is serialized, the XmlSerializer class determines the type of that attribute based on the Instrument class’ definition, using the type attribute for each column. Since theinstrument_idattribute does not exist in the class definition, a nil object is returned which, as of Ruby 1.9, does not have atypeattribute, which Rails is depending on.(I don’t think the patch in the thread you linked to works — but the one I’ve provided below does.)
There are two ways to fix this:
instrument_id(good idea).