Searching in Ruby on Rails using OR logic

Hello there! Currently, I am utilizing AngularJS alongside Ruby on Rails for the backend. I am curious about how to utilize the find method to search for an object array using one of two specific words.

console.log(_($scope.reasonOfRejection).find({name: {en: 'Black Flagged'}} || {name: {en: 'Black Flag'}}))

Answer №1

To locate a specific item by name, you should use the find_by method instead of find, like this:

User.find_by(name: ["Black Flagged", "Black Flag"])

Moreover, if you are working with a JavaScript array using the native find method and assuming the array is _($scope.reasonOfRejection), you can implement it this way:

_($scope.reasonOfRejection).find(({ name }) => (
  name.en === "Black Flagged" || name.en === "Black Flag"
))

The find function will return the first item that meets the specified condition. If you wish to retrieve an array of all matching items, you should use the filter method instead.

const array = [{name: { en: "another item"}}, {name: { en: "item"}}, {name: { en: "Black Flagged"}}]
const match = array.find(({ name }) => (
      name.en === "Black Flagged" || name.en === "Black Flag"
    ))
    
    console.log(match)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

ReactJS bug: Array rendering problem affected by recent changes

Why does ReactJS remove the first element instead of the middle element when using array.splice to remove an element from an array? This is my code. I am using Redux as well. const reducerNotesAndLogin = (state = initialState, action) => { var tableNo ...

jQuery Validation for Radio Buttons and Optional Fields

I've been pondering over this issue for a couple of hours now. Currently, my focus is on validating a form using a jQuery plugin. Here's the link to the documentation: http://docs.jquery.com/Plugins/Validation Here's the code I have so fa ...

Tips for sending a JavaScript parameter to PHP

I am implementing a pop-up modal window that retrieves data from the myForm and saves the email field value to a JavaScript variable in index.php. How can I pass this JavaScript value to PHP and display it using echo, without refreshing the index.php windo ...

Is there a way to stop the continuous loading of AJAX requests?

For weeks, I have been facing a persistent issue that I've attempted to solve in various ways. The problem lies in my ajax search function. Every time I initiate a search, it continues loading multiple times. Surprisingly, the more searches I perform ...

Karaf (Predix 14.3) error encountered: 413 FULL head - 413 (Request Entity Too Large) - Please check the request size

My current architecture setup looks like this: BROWSER <-> Play Framework 2.2.1 + AngularJS <-(REST)-> Karaf Everything is working fine in this configuration. However, when I introduce: BROWSER <-> Apache Reverse Proxy <->Play Fr ...

Elements listed are often ignored by HTML

My website sends a URL and xpath query to the server in order to extract data from the URL based on the xpath query. While it is functioning properly, I am encountering a singular issue. When I specify an xpath query for href,text, it displays a list of a ...

Leveraging the power of javascript to include content before and after

I am looking to understand how I can insert an HTML element before and after certain elements. For example, let's say we have the following code in a real file: <ul class="abcd" id="abcd></ul> How can I display it like this using JavaScr ...

Can anyone offer me advice on troubleshooting a failed Capifony deployment caused by a time out during the assetic:dump process?

$ cap deploy Unfortunately, the deployment process is failing and I am receiving the following error message: * executing "cd /var/www/site/prod/releases/20120831164520 && php app/console assetic:dump web --env=prod --no-debug" servers: ["site ...

How to Align Text at the Center of a Line in Three.js

Exploring What I Possess. https://i.sstatic.net/nAtmp.png Setting My Goals: https://i.sstatic.net/svcxa.png Addressing My Queries: In the realm of three.js, how can I transform position x and y into browser coordinates to perfectly align text in th ...

Method for transmitting JSON array from Controller to View using CodeIgniter

I have a function in my controller: function retrieveAllExpenses() { $date=$this->frenchToEnglish_date($this->input->post('date')); $id_user=$this->session->userdata('id_user'); $where=array('date&ap ...

Tips for waiting for an HTML element to load in a Selenium JavaScript testing script

I'm struggling to find a way to wait for an element to load in a javascript selenium test script. The closest thing I've come across is until.elementLocated, but it seems to throw an immediate exception. Is there a method to delay throwing the " ...

Issue encountered in Next.JS when attempting to validate for the presence of 'window == undefined': Hydration process failed due to inconsistencies between the initial UI and the server-rendered

I encountered an issue that says: Hydration failed because the initial UI does not match what was rendered on the server. My code involves getServerSideProps and includes a check within the page to determine if it is running in the browser (window==&apo ...

Tips for identifying changes in APIs during the simulation of end-to-end tests?

I am seeking to establish a strong e2e testing framework for our team's project, but I am struggling to find a straightforward solution to the following question: When all calls are mocked, how can we effectively detect if the actual model of the obj ...

Encountering difficulties in compiling Dynamic HTML with the $compile function

I'm attempting to incorporate dynamic HTML into my code with the following lines: var el = $compile('<a ng-controller=\"tableController\" ng-click=\"open\">...ReadMore</a>')($scope); But I'm encounterin ...

Trouble with predefined JavaScript in Mongodb situation

Encountering the error "Missing ";" before statement" in Mongodb Atlas Online is frustrating for me as a newbie. Despite my efforts, I can't seem to figure out why the following code snippets are causing this issue: const counter = await counterCollec ...

Easily load events into a responsive calendar widget with w3widgets. No

When attempting to load events dynamically, I encountered an issue where the output was not displaying correctly. I used AJAX to retrieve data in the following format: var datalist = "2015-09-22":{} $(".responsive-calendar").responsiveCalendar({ eve ...

How can you swap out a forward slash in vue.js?

I am facing a coding issue that I need help with: <template slot="popover"> <img :src="'img/articles/' + item.id + '_1.jpg'"> </template> Some of the numbers in my item.id contain slashes, leadin ...

Ways to import JavaScript into child HTMLs embedded within ng-view

I'm diving into angular for the first time and I need to figure out how to incorporate JavaScript into my HTML within ng-view. Some suggestions I came across mention adding jQuery. Can someone provide some guidance on specifically what needs to be ad ...

Change this npm script into a gulp task

I have a script in npm that I would like to convert into a gulp task. "scripts": { "lint": "eslint .", "start": "npm run build:sdk && node .", "posttest": "npm run lint && nsp check", "build:sdk": "./node_modules/.bin/lb- ...

What are the steps to fix the CORS problem when sending AJAX requests from a frontend to a Flask server?

My current project involves creating a web application with Flask for the backend and JavaScript for the frontend. However, I'm encountering challenges with CORS policy when attempting to send AJAX requests from my frontend to the Flask server. Below ...