h-lame / parental_control

A plugin for rails that allows has_one, has_many and certain belongs_to associations to share instances of the "parent" model to the "child" model via the association.
MIT License
30 stars 7 forks source link

Association can't be used in default_value_for block #3

Open pepawel opened 15 years ago

pepawel commented 15 years ago

I'm using nice Phusion's default_value_for plugin. It is not possible to access parent object from within child's default_value_for block. This is because parental control fixes the association after object initialization, while default_value_for wraps initialize method. Example (this time checked in console);

class Face < ActiveRecord::Base
  default_value_for(:smiled) { |face| face.man.happy }
end

man = Man.new :happy => true
man.build_face

One may expect this code to return Face instance with :smiled attribute set to true. Instead exception is raised, as man is not set for face yet.

Is is possible to modify parental_control plugin to fix the association earlier?

h-lame commented 15 years ago

Unfortunately, not without a lot more monkey patching of how active record works.

I'll have a think about it, as I think that in some cases we do want the Man instance set of Face instances earlier than it currently does it - I'm just not sure how yet. If you have any ideas, please do let me know.