Im using ruby 1.9.2 and Rails 3.0.3.
I have the following models
class Property < ActiveRecord::Base
class Feature < ActiveRecord::Base
class DefaultFeature < Feature
class CustomFeature < Feature
class PropertyFeature < ActiveRecord::Base
One Property can have many Features, and one Feature is associated with
Properties (joined through PropertyFeature model). There are two kinds
Features: DefaultFeatures and CustomFeatures, for what Im using single
inheritance. Both kinds of Features have exactly the same fields, but i
to differentiate them in my application.
What i want to achieve, if possible, is being able to access Features
Property instance like this:
And also add features to a property:
a_property.custom_features << CustomFeature.new
a_property.features << CustomFeature.new
The closest solution i could think of is this one:
The problem I’m having is that when i call a_property.custom_features it
brings all features from the property: custom AND default ones.
I’ve already tried making polymorphic the belongs_to :feature
in the PropertyFeature model, but it raised an
ActiveRecord::HasManyThroughAssociationPolymorphicError exception, so i
assume rails is not supposed to work like that.
I’d appreciate any help on how to do this (i’ve tried everything i could
think of). Maybe my approach is wrong to begin with, in which case i’d
to hear a better way to to this.