Ruby ternary operator not returning as expected

asdlfkjlkj

I have following code

@services = []
@services << @user.services.where(active: true).blank? ? @user.services.build(active: true) : @user.services.where(active: true).first

Instead of returning service object I get true or false in the array. Why is that?

Eric Duminil

As you can see in this precedence table, << is higher than ?, so your code is parsed as :

(@services << @user.services.where(active: true).blank?) ? @user.services.build(active: true) : @user.services.where(active: true).first

You could write :

@services = []
@services << (@user.services.where(active: true).blank? ? @user.services.build(active: true) : @user.services.where(active: true).first)

But it's not very readable, and you call where twice.

active_services = @user.services.where(active: true)
service = if active_services.blank?
            @user.services.build(active: true)
          else
            active_services.first
          end
@services = [service]

Update From @mudasobwa's excellent comment :

@services = [@user.services.where(active: true).first || @user.services.build(active: true)]

Update From @Stefan, it looks like your code is equivalent to :

@services = [@user.services.find_or_initialize_by(active: true)]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Ruby ternary operator (or) or operator

From Dev

Ternary operator is not returning undefined

From Dev

Why does returning null (where a boolean is expected) as the result of a ternary operator compile?

From Dev

if condition with ternary operator not returning true or false

From Dev

Ternary operator in LINQ query is not working as expected

From Dev

Ternary operator in LINQ query is not working as expected

From Dev

Ruby single line ternary in function, is returning boolean

From Dev

Using Ruby's ternary operator ? : to shorten this

From Dev

Ruby ternary operator in erb with html tags

From Dev

2-Level Ruby Ternary Operator Not Working

From Dev

Using Ruby's ternary operator ? : to shorten this

From Dev

Ruby - Is it possible to use a ternary operator in a proc?

From Dev

Method not returning the expected value in Ruby

From Dev

Difference between ternary (conditional) operator and if statement returning an Action

From Dev

Can I use the ternary operator with two functions each returning void?

From Dev

Java 8 - Ternary operator returning function doesn't compile

From Dev

Java 8 - Ternary operator returning function doesn't compile

From Dev

Can I use the ternary operator with two functions each returning void?

From Dev

Ruby on Rails ternary operator to display BOTH nil and false

From Dev

+ operator in a ternary operator

From Dev

Ternary operator and increment operator

From Dev

Ternary Operator(Elvis Operator) ?:

From Dev

Ternary operator with "OR" operator not working?

From Java

Why does using the ternary operator to return a string generate considerably different code from returning in an equivalent if/else block?

From Dev

Ternary operator displays error in JSHint - Expected an assignment or function call and instead saw an expression

From Dev

conditional ternary operator is giving error: Expected an assignment or function call and instead saw an expression when setting a variable

From Dev

Ternary operator displays error in JSHint - Expected an assignment or function call and instead saw an expression

From Dev

Ternary operator with append (<<) operator and match

From Dev

Simple ternary operator not working

Related Related

  1. 1

    Ruby ternary operator (or) or operator

  2. 2

    Ternary operator is not returning undefined

  3. 3

    Why does returning null (where a boolean is expected) as the result of a ternary operator compile?

  4. 4

    if condition with ternary operator not returning true or false

  5. 5

    Ternary operator in LINQ query is not working as expected

  6. 6

    Ternary operator in LINQ query is not working as expected

  7. 7

    Ruby single line ternary in function, is returning boolean

  8. 8

    Using Ruby's ternary operator ? : to shorten this

  9. 9

    Ruby ternary operator in erb with html tags

  10. 10

    2-Level Ruby Ternary Operator Not Working

  11. 11

    Using Ruby's ternary operator ? : to shorten this

  12. 12

    Ruby - Is it possible to use a ternary operator in a proc?

  13. 13

    Method not returning the expected value in Ruby

  14. 14

    Difference between ternary (conditional) operator and if statement returning an Action

  15. 15

    Can I use the ternary operator with two functions each returning void?

  16. 16

    Java 8 - Ternary operator returning function doesn't compile

  17. 17

    Java 8 - Ternary operator returning function doesn't compile

  18. 18

    Can I use the ternary operator with two functions each returning void?

  19. 19

    Ruby on Rails ternary operator to display BOTH nil and false

  20. 20

    + operator in a ternary operator

  21. 21

    Ternary operator and increment operator

  22. 22

    Ternary Operator(Elvis Operator) ?:

  23. 23

    Ternary operator with "OR" operator not working?

  24. 24

    Why does using the ternary operator to return a string generate considerably different code from returning in an equivalent if/else block?

  25. 25

    Ternary operator displays error in JSHint - Expected an assignment or function call and instead saw an expression

  26. 26

    conditional ternary operator is giving error: Expected an assignment or function call and instead saw an expression when setting a variable

  27. 27

    Ternary operator displays error in JSHint - Expected an assignment or function call and instead saw an expression

  28. 28

    Ternary operator with append (<<) operator and match

  29. 29

    Simple ternary operator not working

HotTag

Archive