ActiveRecord 3.0.10 how to overwrite initialize from association?

Hello,

I’m trying to implement the EAV pattern in ActiveRecord, for that case
I’ve create two models: Product and ProductType and based on what
ProductType your product is you get dynamically defined accessors.
This way you can use the dynamic attributes by all AR methods, like
#update_attributes, etc. But you can’t directly pass a hash with
dynamic attributes to new.

See the code here:


And the corresponding test:

The test fails with the following:
Failure/Error: product = @pt.products.create :price => 10, :ram
=> 6.0
ActiveRecord::UnknownAttributeError:
unknown attribute: ram
# ./app/models/product.rb:6:in initialize' # ./spec/product_spec.rb:98:inblock (3 levels) in <top
(required)>’

This is because the AR#initialize try to call #ram= which is not
defined yet,
if I first try to define the dynamic attributes I don’t have access to
the product_type relation, interesting thing is that I can’t even
access it from the attributes when called with this:
@pt.products.create

Does anyone have any idea how I can make this pass ?

Thanks,
Evgeni

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs