Reputation: 407
So I have one model called Project, for which there is a nested model called Proposal (so every project has multiple proposals, and each proposal only belongs to one Project).
I have a column for Proposal called "winning" which just checks if one of the Proposals has won for the Project. I'd like to reference this on the Show page of the Project, but a little perplexed by the code.
What I really want to do is check if any of the proposals have status "winning"
This is what I'm trying for the Show view for Projects, but it isn't working:
<% if @project.proposals.winning %>
SUCCESSFUL
<% end %>
I feel like this should be pretty rudimentary but I'm having trouble figuring it out, thanks!
Upvotes: 0
Views: 115
Reputation: 111
That's ideal candidate for:
<% if @idea.proposals.any? {|proposal| proposal.winning? } %>
Enumerable.any? returns true if for any array element the block returns true.
Upvotes: 1
Reputation: 13925
Use it instead:
<% if @idea.proposals.count{|a| a.winning } > 0 %>
Or even better to create a method for it in the Idea model:
def has_winning?
proposals.count{|a| a.winning } > 0
end
Upvotes: 1
Reputation: 407
Okay, found this code on another post and it seems to be working, not sure if it's the best way to go about it though:
<% if @idea.proposals.map(&:winning).flatten %>
Upvotes: 0