Evgeni Dzhelyov
2011-Sep-27 17:54 UTC
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:
https://github.com/edzhelyov/product_manager/blob/master/app/models/product.rb#L5
And the corresponding test:
https://github.com/edzhelyov/product_manager/blob/master/spec/product_spec.rb#L97
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:in `block (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
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.