0

내가 가지고있는 다형성 연관의 두 모델을 통과하는 루프를 만드는 법을 모르겠습니다. 나는 모델이 User, Follow, DadMom입니다. 여러 다형성 모델을 반복하는 방법은 무엇입니까?

# FollowsController 

index 
# follows = dads and moms 
@follows = current_user.follows.paginate(page: params[:page]) 
end 

# follows/index.html.erb 

<% @follows.each do |f| %> 
<%= f.dad.name %> 
<%= f.mom.name %> 
<%= f.mom.address %> 
<% end %> 

방법이 작업을 수행 할 것입니다 :

class User 
has_many :follows 
end 

class Mom 
    has_many :follows, as: :followable 
    # columns: name, :address 
end 

class Dad 
    has_many :follows, as: :followable 
    # columns: name 
end 

class Follow 
belongs_to :user 
belongs_to :followable, polymorphic: true 
# columns: user_id, followable_id, followable_type 
end 

내 목표는 사용자가 다음과가 같은 특정의 속성을 얻을 수 있다는 엄마와 아빠를 통해 루프인가?

+0

각각의 반복처럼 확인할 수 있습니다 당신이 아빠와 엄마가 다음 있는지 확인하려면

f.followable.name f.followable.address 

를 사용할 필요가 당신은 아빠 관계 또는 엄마 관계를 가지고 있습니다, 한번에 둘 다 결코하지 마십시오 .... f.followable_type == "아빠"라면 <% = f.dad.name은 어떨까요? %> – SteveTurczyn

+0

@SteveTurczyn 그래서 피드에 두 가지를 넣을 실제 방법이 없습니까? – user2784630

+0

current_user에는 많은 아빠와 엄마가 있습니까? 그렇다면 아빠와 엄마는 서로 관련이 있거나 어떻게 든 연결되어 있습니까? – SteveTurczyn

답변

1

당신은 당신이

f.followable_type == 'Dad' 

또는

f.followable.kind_of?(Dad) 
관련 문제