Ruby on Rails ternary operator to display BOTH nil and false

Iggy

I have 3 roles for User, call it standard, premium, and admin. Each can post posts. Premium and admin can choose whether to make private post or non private post (private: false or private: true). Standard user will not be given the option to choose whether their Post will be private/ not; by default, all of standard user's posts will have Private: nil status.

To summarize:

  • Standard user's posts will always be private: nil
  • Premium/ Admin user's posts can either be private: true or private: false

I am trying to make a scope so that when a standard user views index page, they see posts with Private: nil and Private: false, while Premium and Admin user can view all. My problem is, I can't come up with a ternary operators that allows standard users to view both private: nil and private: false Posts.

This is what I have on Post model:

  scope :visible_to, -> (user) { user.admin? || user.premium? ? all : where(private: false) || where(private: nil)}

The code above only displays Posts with private: false. I've tried various combination:

... where(private: false || nil)}

Displays only private: false posts

... where(private: nil || false)}

Displays only private: nil posts

... where(private: nil && false)}

Displays only private: nil posts

... where(private: false && nil)}

Displays only private: false posts

and several other combinations -

I also noticed that when I switched the order of false and nil, it displays different results. I thought || or && operators are non-commutative. Maybe there is something about scoping or ternary operators that I was not aware...

Thanks!

Alex Bezek

It sounds like your just wondering how to do an or operator in a where? http://guides.rubyonrails.org/active_record_querying.html

Section 2.3.3 Subset Conditions

... where(private: [false, nil])}

This should convert it into the sql

WHERE private in (false, nil)

Which is shorthand for multiple or statements in sql. You can also use arel tables for more advanced sql features without breaking down to pure sql strings. Checkout out this article about your case using IN along with arel examples

https://robots.thoughtbot.com/using-arel-to-compose-sql-queries

# It even handles arrays containing nil!
where(foo: ['bar', 'baz', nil]) # => (WHERE foo IN ('bar', 'baz') OR foo IS NULL)

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

Ruby ternary operator not returning as expected

From Dev

Ternary operator within Rails view

From Dev

replace ternary nil check with ruby best practices

From Dev

if condition with ternary operator not returning true or false

From Dev

Ternary Operator Outputting True Option when False

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

Ternary operator as both left operative and right operative.. or neither

From Dev

Must both right-hand expressions of a ternary operator be of compatible types?

From Dev

Ruby on Rails Testing for Nil - NoMethodError Nil:class

From Dev

Why is the conditional (ternary) operator evaluated in a logical AND when the lhs is false

From Dev

Object returns `false` for both `true` and `false` with the equality operator

From Dev

return nil vs return false in Ruby

From Dev

How to correctly return the path to an image from the ternary operator (and display it)

From Dev

Ruby on Rails - Ruby Operator Precedence - Parentheses

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 Dev

nil can't be coerced into BigDecimal - Ruby On Rails

From Dev

Ruby on Rails Devise Confirmation Token is nil

From Dev

Ruby on rails, multiple check for nil attributes

From Dev

Ruby on Rails - undefined method `persisted?' for nil:NilClass

From Dev

Nil errors in creating a hash in Ruby on Rails

From Dev

undefined method `each' for nil:NilClass - Ruby on Rails

Related Related

  1. 1

    Ruby ternary operator (or) or operator

  2. 2

    Ruby ternary operator not returning as expected

  3. 3

    Ternary operator within Rails view

  4. 4

    replace ternary nil check with ruby best practices

  5. 5

    if condition with ternary operator not returning true or false

  6. 6

    Ternary Operator Outputting True Option when False

  7. 7

    Using Ruby's ternary operator ? : to shorten this

  8. 8

    Ruby ternary operator in erb with html tags

  9. 9

    2-Level Ruby Ternary Operator Not Working

  10. 10

    Using Ruby's ternary operator ? : to shorten this

  11. 11

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

  12. 12

    Ternary operator as both left operative and right operative.. or neither

  13. 13

    Must both right-hand expressions of a ternary operator be of compatible types?

  14. 14

    Ruby on Rails Testing for Nil - NoMethodError Nil:class

  15. 15

    Why is the conditional (ternary) operator evaluated in a logical AND when the lhs is false

  16. 16

    Object returns `false` for both `true` and `false` with the equality operator

  17. 17

    return nil vs return false in Ruby

  18. 18

    How to correctly return the path to an image from the ternary operator (and display it)

  19. 19

    Ruby on Rails - Ruby Operator Precedence - Parentheses

  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

    nil can't be coerced into BigDecimal - Ruby On Rails

  25. 25

    Ruby on Rails Devise Confirmation Token is nil

  26. 26

    Ruby on rails, multiple check for nil attributes

  27. 27

    Ruby on Rails - undefined method `persisted?' for nil:NilClass

  28. 28

    Nil errors in creating a hash in Ruby on Rails

  29. 29

    undefined method `each' for nil:NilClass - Ruby on Rails

HotTag

Archive