2009-11-15 4 views
0

j'ai modèle représentent la règle d'association (Body => Head)problème trouver table jointe dans des rails

def Item 
has_many :heads 
has_many :bodies 
... 
end 

def Rule 
has_many :heads 
has_many :bodies 
... 
end 

def Body 
belongs_to :item 
belongs_to :rule 
... 
end 

def Head 
belongs_to :item 
belongs_to :rule 
... 
end 

Je veux trouver la règle qui ont l'objet de corps Articles appropriés spécifié et que vous souhaitez accéder à la tête par la règle mais je ne peut pas faire comme

def Rule 
has_many :heads 
has_many :bodies 
has_many :item, :through => :heads 
has_many :item, :through => :bodies 
... 
end 

Que dois-je changer et faire pour accomplir ceci?

Merci,

Répondre

0

Enfin je viens avec cette solution

class Rule 
    has_many :heads 
    has_many :bodies 
    ... 
end 
class Body 
    belongs_to :rule 
    has_many :items, :as => :rulable 
end 
class Head 
    belongs_to :rule 
    has_many :items, :as => :rulable 
end 
class Item 
    belongs_to :rulable, :polymorphic => true` 
end 

Ne pas sûr que ce soit une bonne solution, mais ce que je l'utilise pour l'instant.

0
def Rule 
    has_many :heads 
    has_many :bodies 
    has_many :head_items, :through => :heads 
    has_many :body_items, :through => :bodies 
    ... 
end 

Vous devez différentes associations has_many pour chacun.

+0

Je devrais avoir les modèles head_items et body_items comme héritage des articles? – sarunw

Questions connexes