Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

ActiveResource custom class namespace bug

#6716

ActiveResource has a bug finding custom defined classes for attributes in the same namespace as the current model. For some reason when it looks for pre-defined classes, it won't look in the current namespace, but that is where it will define a new model if the class does not exist.

If my resource comes back as:

<person>
  <first-name>John</first-name>
  <last-name>Doe</last-name>
  <address>
    <street>123 Park Avenue</street>
    <city>New York</city>
    <state>NY</state>
  <address>
</person>

This works and will dynamically create a new class Person::Address < ActiveResource::Base for person.address:

class Person < ActiveRecord::Base
end

This code also works and person.address will use the Address class and address will not be treated as a resource:

class Person < ActiveRecord::Base
end

class Address
  attr_reader :attributes
  def initialize(attributes = {})
    @attributes = attributes
  end
end

However, this code will not work. ActiveResource will try to use the Person::Address class, but it will treat it as a resource resulting in method missing errors when it tries to call prefix= and site= on it:

class Person < ActiveRecord::Base
  class Address
    attr_reader :attributes
    def initialize(attributes = {})
      @attributes = attributes
    end
  end
end

Reported by Brian Durand · April 18th, 2011 @ 08:32 PM

State: new
Milestone: none
Assigned to: nobody
Importance: none