ruby on rails - How do you get the parent of a polymorphic object before its been saved? -


i have standard polymorphic relationship , need know parent before save it.

class picture < ar::base   belongs_to :attachable, :polymorphic => true end  class person < ar::base   has_many :pictures, :as => :attachable end  class vehicle < ar::base   has_many :pictures, :as => :attachable end 

i'm uploading pictures via paperclip , build processor needs different things different pictures (ie. person pictures should have polaroid & vehicle pictures should have overlay). problem before picture saved don't know if associated person or vehicle.

i tried putting "marker" in person & vehicle tell them appart, when i'm in paperclip processor thing see picture class. :( next thought climb stack try , parent caller seems quite smelly me. how it?

you should able polymorphic association.

class picture < ar::base   belongs_to :attachable, :polymorphic => true    before_create :apply_filter    private    def apply_filter     case attachable     when person       #apply person filter     when vehicle       #apply vehicle filter     end   end end 

or, can ask association type dosen't have build , compare objects, rather string comparison.

class picture < ar::base   belongs_to :attachable, :polymorphic => true    before_create :apply_filter    private    def apply_filter     case attachable_type     when "person"       #apply person filter     when "vehicle"       #apply vehicle filter     end   end end 

Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -