Hi,
first of all I would like to wish a happy new year to all the list
members.
Here is my problem. I have a model class where I need to overwrite an
accessor method in order to add some extra behaviour:
class LineItem < ActiveRecord::Base
belongs_to :item, :polymorphic => true
add_item
def item=(item)
write_attribute(:item, item)
self.price = item.price
end
item
def item
read_attribute(:item)
end
end
The problem is that when I overwrite the item= method, the item_id and
item_type attributes aren’t stored in the database when I save the
object so the associated item is missed. I know I could manually set the
item_id and item_type values with something like this:
def item=(item)
write_attribute(:item, item)
self.price = item.price
self.item_id = item.id
self.item_type = item.class.to_s
end
but the problem with this solution resides on that the given item
probably has no id as it should handled as a new record too.
Is there any way I could use the default association behaviour(that is
to automatically set the item_id and item_type attributes and saving the
item first when necessary) and still being able to overwrite the writer
method too? I also tried it by aliasing the item= method but with no
success.
Thanks in advance.
–
/**
- dagi3d v4 | http://dagi3d.net
*/