How to find by array and return nil if not matched?

Tintin81

I have this before_filter in one of my controllers:

def valid_people
  person_ids = params[:project][:person_ids]
  if person_ids.present?        
    person = current_user.people.where("id IN (?)", person_ids).to_a
    redirect_to(root_path) unless person
  end
end

Line 4 checks if all the ids in the person_ids array are contained in the user's people.

However, it throws an error if that's not the case.

How can I get line 4 to return nil instead?

Thanks for any help.

spickermann

Check for present? or blank?. That also handles empty arrays:

redirect_to(root_path) if person.blank?

Btw: The variable person should be renamed to people, because it returns an array for people not just one person.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to use lodash to find and return an object from Array?

From Java

How to Return Nil String in Go?

From Dev

How to return matched results in ActiveRecord and then remainder?

From Dev

How to return pattern-matched element?

From Dev

What is the appropriate return value when the pattern matched is Nil and we want to return Nil?

From Dev

How to return just the matched elements from a mongoDB array

From Dev

How to return matched regex in R gsub

From Dev

How to loop through Array of Objects to find ID then return a sibling key

From Dev

How to add nil into Array?

From Dev

How to return matched value with .NET regex?

From Dev

Meteor: How to find array of object with matched array of selector?

From Dev

How to remove element from array if values matched?

From Dev

How to find array items that begin with a number and return it in reverse alphabetical order?

From Dev

Iterate over an array and return only matched values?

From Dev

Return Object in Array based on matched data

From Dev

TypeScript: how to find an object in an array and return that specific object?

From Dev

How to make MongoDB find nested json return as an array?

From Dev

How to return 0 if a pattern is matched from a file?

From Dev

How to return 0 if a pattern is matched from a file?

From Dev

How to return pattern-matched element?

From Dev

How to return the column head if a cell is matched?

From Dev

How to return matched regex in R gsub

From Dev

How to find index of array of records return from database in rails?

From Dev

How to add nil into Array?

From Dev

Return matched array items and remove them from original array

From Dev

How to find, return and remove the maximum value from an array in Ruby?

From Dev

How to match array with object to retrieve matched elements?

From Dev

How to find fuzzy matched commands in bash

From Dev

How to find pattern in javascript variable and return the founded element in array?

Related Related

  1. 1

    How to use lodash to find and return an object from Array?

  2. 2

    How to Return Nil String in Go?

  3. 3

    How to return matched results in ActiveRecord and then remainder?

  4. 4

    How to return pattern-matched element?

  5. 5

    What is the appropriate return value when the pattern matched is Nil and we want to return Nil?

  6. 6

    How to return just the matched elements from a mongoDB array

  7. 7

    How to return matched regex in R gsub

  8. 8

    How to loop through Array of Objects to find ID then return a sibling key

  9. 9

    How to add nil into Array?

  10. 10

    How to return matched value with .NET regex?

  11. 11

    Meteor: How to find array of object with matched array of selector?

  12. 12

    How to remove element from array if values matched?

  13. 13

    How to find array items that begin with a number and return it in reverse alphabetical order?

  14. 14

    Iterate over an array and return only matched values?

  15. 15

    Return Object in Array based on matched data

  16. 16

    TypeScript: how to find an object in an array and return that specific object?

  17. 17

    How to make MongoDB find nested json return as an array?

  18. 18

    How to return 0 if a pattern is matched from a file?

  19. 19

    How to return 0 if a pattern is matched from a file?

  20. 20

    How to return pattern-matched element?

  21. 21

    How to return the column head if a cell is matched?

  22. 22

    How to return matched regex in R gsub

  23. 23

    How to find index of array of records return from database in rails?

  24. 24

    How to add nil into Array?

  25. 25

    Return matched array items and remove them from original array

  26. 26

    How to find, return and remove the maximum value from an array in Ruby?

  27. 27

    How to match array with object to retrieve matched elements?

  28. 28

    How to find fuzzy matched commands in bash

  29. 29

    How to find pattern in javascript variable and return the founded element in array?

HotTag

Archive