How to conditionally avoid 'nil can't be coerced into Integer' error
Using the code below, I'm getting a 'nil can't be coerced into Integer' error in Rails.
In my view:
<%= account_totals %>
In a view helper:
def account_totals if @user.accounts.any? account_total = 0 @user.accounts.each do |account| account_total += account.amount end account_total end end
It's happening because the user has no accounts associated with them, so 'account.percent' is nil. However, I'm first checking to see if the user has any accounts, so this code should not be running if they don't have any accounts.
What's the right way to sum up the total amounts from a user's accounts?
Because some of the
account records have
nil amount, you can convert them to
integer by calling
to_i or to float by
account.amount is returning nil somewhere, do this
account_total += account.amount.to_i
It will resolve the error. But since
amount is a numeric column, you should better set the default value as 0 in migrations at DB level.
account_total = @user.accounts.sum(:amount)
nil values and runs a SQL
Why you should avoid conditional statements, Why you should avoid conditional statements. 16 September 2015. Developers tend to use a lot of conditional statements in their code. Of course, each flow has � On the Format menu, click Conditional Formatting. In the Conditional Formatting dialog box, click Condition 1, and then click Formula Is. In the edit box to the right of Condition 1, type the following formula: =ISERROR(A1) Click the Format button. In the Format Cells dialog box, click Color, and then click the White square.
def account_totals @user.accounts.map(&:amount).compact.sum end
If there are no accounts,
@user.accounts will be an empty collection. Empty collections still respond to all of the same methods as populated ones, so the
sum call will work - they will just be empty.
nils can be removed from a collection with
To be honest, your question is kind of confused; you refer to
account.percent which is not in your code snippet; and if the
accounts collection is empty then the
+= will NOT run. Since you are getting the coercion error we have to assume that this code IS running, and
nil. Consider defaulting
0 in the Account model if that makes sense. Otherwise, use
compact to filter
nils out of arrays like I say above. IF you DO default
0, you can use the snippet in @AbM 's comment for maximum clarity and minimalism.
How to conditionally avoid property from serializing in WCF?, If I've understood your requirement, the following should work. The property ValueIfFlagTrue is for serialization only, name it as you wish. You can do so as follows: Select C5:E9. The range you initially select is critical to your expectations. In this case, the expectation is to apply Click the Home tab. In the Styles group, click Conditional Formatting and then choose New Rule. In the top pane of the resulting dialog, select Use A
Just do it on the database:
@users = User.select('users.*', 'SUM(accounts.amount) AS account_totals') .left_joins(:accounts) .group(:id)
Most dbs will coalesce null values into 0 when using SUM.
If you are just fetching a single user and two queries is ok you can use
@user = User.find(1) account_totals = @user.accounts.sum(:amount)
Don't do this while looping through a collection of users though as it creates a n+1 query issue.
How to avoid a conditional formatting rule in Excel that doesn't work , Formulaic conditional formatting rules in Microsoft Excel can be tricky, so learn how to avoid a common mistake. How to conditionally avoid property from serializing in WCF? Ask Question Asked 2 years, 6 months ago. Active 2 years, 6 months ago. Viewed 1k times 2. 3. Is it
def account_totals # Initalizing total to 0 account_total = 0 if @user.accounts.any? @user.accounts.each do |account| account_total += account.amount end end #Allways return total, that wil be 0 or other value account_total end
In you code, if the user don't has accounts nil is returned
Understanding Excel's conditional formatting rules can help prevent , Avoid frustration and unintended formatting by learning how Excel applies conditional formatting rules. scanrailistock-469667229.jpg. How to Avoid Conditional JOINs in T-SQL Relational databases go out of their way to execute SQL, however bad the crimes against Codd and relational theory within the query. The 'conditional join', can be executed but at great cost. As always, it is much better to sit back and restate the problem in a set-based way.
The same as above, it gives the blank cells a no format set conditional rule. 1. Select the range you have added conditional formatting. 2. Then go to the Home tab, click Conditional Formatting > Manage Rules. 3. In the Conditional Formatting Rules Manager dialog box, click the New Rule button. 4.
Ignore blank cells in conditional formatting in Excel After creating conditional formatting rules for the list of data, you need to add a new rule to ignore the blank cells in the list. 1. Keep staying in the Conditional Formatting Rules Managerdialog box, then click the New Rulebutton.
account_totalscan simply be
@user.accounts.sum(:amount), assuming that
amountis a column in the database.
- If you have a large number of records this will explode. It's better to let your database do the work on this one.
- Great point @JoshBrody , definitely important to keep in mind. When it comes to stack overflow and I don't have lots of context I prefer to code for brevity and recommend that performance bottlenecks are analyzed once they are a problem - in particular I wasn't certain that accounts were in the database here. But senior engineers that know their data models can preempt those performance problems with your answer