For some reason, the when @orgs is not working :
@orgs = Organization.all.select{|a|a.active}.count
case collection.size
when 0; "No #{entry_name.pluralize} found"
when @orgs; "#{@orgs} Businesses Returned!"
else; "#{collection.total_entries} of #{@orgs} Businesses Returned!"
end
Is this syntactically accurate? It will always return the last else statement. It never catches on the second when statement, even if @orgs == @orgs.
The actual number of @orgs = 1211.
So if I make the when statement when 1211; , it still doesn’t catch. Is there a syntactical mistake here?
Your syntax isn’t the issue. The logic in your conditions, I think, is all messed up.
So, @orgs does not = 1211, @orgs is a collection whose size is 1211. Big difference.
collection.size, when it should probably be@orgs.sizeLet’s say we changed it to be:
Then:
whenstatement would start working when@orgs.size == 0whenstatement would still fail, because@orgs.size != @orgs– and it never will.@orgsrefers to the collection of objects, while@orgs.sizerefers to the size of that collection. Even when@orgs == nilit will still fail because@orgs.sizewill throw a error because you’re calling thesizemethod on anilobject, which isn’t allowed.elsestatement is always the one called because of the problems listed above, and because the way you’ve written it prevent any of the other cases from ever being true.However, that’s probably still not enough. It looks to me like you’re trying to return singularized or pluralized text depending on how many you have. Here’s the code you probably intended to write, without the semi-colon usage:
This will pluralize even when the count is
0, so it will say0 active Organizations of a total 125 Organizations!… or when the value is
1it will say1 active Organization of a total 125 Organziations!If you don’t want
Organizationcapitalized, you can add a call to.downcaseto take care of that.