This project is archived and is in readonly mode.

#6716 new
Brian Durand

ActiveResource custom class namespace bug

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

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

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Attachments

Pages