jeudi 23 avril 2015

Set has_many or has_one dynamically according to object own attributes

In a class I need to dynamically set has_many or has_one association according to the object *own attributes (so the foreign object won't need to change).

Something like:

class Child < ActiveRecord::Base
   if orphan == true #<-- I can't find the good solution for this condition
     has_one :parent
   else 
     has_many :parents
   end
end

on class "Parent" class I need to keep:

class Parent < ActiveRecord::Base
  belongs_to :children #this is true if the child is orphan or not
end

Is there a way to do so?

Just in case: I'm using rails 3.2.14

Aucun commentaire:

Enregistrer un commentaire