2011-05-11 5 views
1

has_many 관계를 선언하는 모델이 있습니다. ActiveRelation에서 사용할 수있는 메타 데이터가 있습니까? has_many 관계를 통해이 모델을 작업 할 때 has_many 관계가 모델에 포함되어 있는지 확인한 다음 거기에서 각각의 내용에 액세스 할 수 있습니까?레일스 has_many 메타 데이터

일부 의사 코드 (실행되지 않습니다) 내 목표는 무엇인지 명확히 도움이된다면 : 당신이 할 수 물론

MyModel.has_many_relationships.each do |relationship| 
    relationship.contents.each do |content| 
     # ... 
    end 
end 

답변

2

!

레일스 문서에서 "reflect_on_all_associations"를 검색해보십시오!

reflect_on_all_associations(macro = nil) 

Returns an array of AssociationReflection objects for all the associations in the class. If you only want to reflect on a certain association type, pass in the symbol (:has_many, :has_one, :belongs_to) as the first parameter. 

Example: 

    Account.reflect_on_all_associations    # returns an array of all associations 
    Account.reflect_on_all_associations(:has_many) # returns an array of all has_many associations 
+0

완벽합니다. 감사합니다 Aditya! – inyourcorner

+0

그리고 결국 효과가 있다면 어쩌면 답을 받아 들일 수도 있습니다! 건배! :) –