Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActiveRecord where field = ? array of possible values

I want to do

Model.where('id = ?', [array of values]) 

How do I accomplish this look up without chaining OR statements together?

like image 581
quantumpotato Avatar asked Mar 10 '15 01:03

quantumpotato


People also ask

Is ActiveRecord an ORM?

ActiveRecord is an ORM. It's a layer of Ruby code that runs between your database and your logic code.

What is ActiveRecord naming convention?

ActiveRecord expects applications to follow certain naming conventions. These conventions extend from file naming, class naming, table naming and more. By default classes are singular, tables are plural, primary keys are id and foreign keys are table_id.

What does ActiveRecord base do?

Active Record can serialize any object in text columns using YAML. To do so, you must specify this with a call to the class method serialize. This makes it possible to store arrays, hashes, and other non-mappable objects without doing any additional work.

What is an ActiveRecord relation object?

An instance of ActiveRecord::Base is an object that represents a specific row of your database (or might be saved into the database). Whereas an instance of ActiveRecord::Relation is a representation of a query that can be run against your database (but wasn't run yet).


1 Answers

From here it appears to be done using an SQL in statement:

Model.where('id IN (?)', [array of values]) 

Or more simply, as kdeisz pointed out (Using Arel to create the SQL query):

Model.where(id: [array of values]) 
like image 126
Will Richardson Avatar answered Oct 14 '22 11:10

Will Richardson