0

What is the effective way of this code?I think there should be better way.I wanna re-code this.

if @project.contest_entries.where("view_in_showcase = ?", true)
 entries = @project.contest_entries.where("view_in_showcase = ?", true).count
else
 entries = 1 
end
4
  • Why not just count, and if it's zero, set to one? (Which seems weird to me.) Commented Jan 30, 2013 at 0:26
  • @DaveNewton: Zero would not trigger the else clause in Ruby. Commented Jan 30, 2013 at 0:27
  • @Amadan ... I guess I would have thought it obvious you wouldn't use the same logical condition. Commented Jan 30, 2013 at 0:29
  • It's not clear to me if you understand that this code is fundamentally broken if you ever expect entries to be anything other than 0 or the count of contest_entries. The entries = 1 branch will never execute. This makes the code very misleading; I'd consider fixing it. Commented Jan 30, 2013 at 0:43

2 Answers 2

3

You could use max:

entries = [1, @project.contest_entries.where(view_in_showcase: true).count].max

I would define a scope on ContestEntry to get rid of that where clause though:

scope :showcased, where(view_in_showcase: true)

Then that would become

entries = [1, @project.contest_entries.showcased.count].max
Sign up to request clarification or add additional context in comments.

1 Comment

+1 for the scope, which is probably the preferred generic solution.
0
showcased_project_entries =
    @project.contest_entries.where("view_in_showcase = ?", true)
entries = showcased_project_entries ? showcased_project_entries.count : 1

or

entries =
    @project.contest_entries.where("view_in_showcase = ?", true).try(:count) || 1

Although, I must admit I am not sure under which circumstances where returns a falsy value.

EDIT: As noted in the comments, the else clause indeed never triggers, so your code probably does not do what you want. See Andy H's solution for the case where you want to have entries be 1 when you find no results, if that is what you meant.

8 Comments

I don't think this would work, where would return [] if there are no entries matching the condition.
Not here; where will return an empty array, which won't satisfy the ternary. Hence counting, and if the result is 0, set to one.
Yeah, I suppose so. I'll edit it in... although what I wrote is equivalent to the OP's code, and I'm not a mind-reader.
Not sure how mind-reading comes in to it, but if you run the code (or similar code using an existing project's models), it's easy enough to test, and AFAICT this code won't work: with no results the if condition still evaluates to true, and try(:count) will work, you'll just call it on an empty array.
@DaveNewton: Which is exactly what would happen in OP's code. It comes down to "make this same, but more pretty and Rubyish" vs. "make this correct because what I wrote doesn't work"; I understood the question to be former, and my answer was a straight rewrite without changing the functionality.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.