hzamani / acts_as_relation

Multi table Inheritance for rails
http://hzamani.github.com/acts_as_relation/
MIT License
180 stars 58 forks source link

gem doesn't work when using namespaces #17

Closed marcosholgado closed 12 years ago

marcosholgado commented 12 years ago

Hi,

I've just tried to write down your sample but using a namespace Stationery. My model is basically same as yours but I added a Catalogue which can have many products and a belongs to catalogue inside the Product model.

class Stationery::Catalogue < ActiveRecord::Base attr_accessible :name has_many :products, :dependent =>:destroy end

class Stationery::Product < ActiveRecord::Base attr_accessible :name, :price acts_as_superclass belongs_to :catalogue end

class Stationery::Book < ActiveRecord::Base attr_accessible :title acts_as :product end

class Stationery::Pen < ActiveRecord::Base attr_accessible :color acts_as :product end

I get this error:

uninitialized constant ActiveRecord::ActsAsModules::ActsAsProduct::Product (NameError)

I can't figure out exactly what's going on but I'm pretty sure is something about the namespaces, in the error the namespace is missing so I thing that could be the reason, when I'm not using namespaces everything works fine.

BR!

marcosholgado commented 12 years ago

Hi again,

I've found a "solution" which is probably not really elegant but it works so far on the tests I have. Basically on the acts_as you have to add the class_name and also the as option since it seems that using a class_name with a namespace messes something up on the superclass table. So finally I have:

class Stationery::Catalogue < ActiveRecord::Base attr_accessible :name has_many :products, :dependent =>:destroy end

class Stationery::Product < ActiveRecord::Base attr_accessible :name, :price acts_as_superclass belongs_to :catalogue end

class Stationery::Book < ActiveRecord::Base attr_accessible :title acts_as :product, :class_name => "Stationery::Product", as: => :product end

class Stationery::Pen < ActiveRecord::Base attr_accessible :color acts_as :product, :class_name => "Stationery::Product", as: => :product end

On the database I had to specify the product_id and product_type and remove the :as_relation_superclass => true since without the :as => :product it didn't add automatically those variables and always gave me an error like "as_product_id" and so on.

BR