Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActiveRecord join with Array of IDs

The following command is useful in pulling out all Styles with a certain feature:

Style.joins(:style_features).where('style_features.feature_id= ?', 1)

Is it possible to do the same thing, but for a series of features? As in:

Style.joins(:style_features).where('style_features.feature_id= ?', [1, 2, 3])
like image 977
Abram Avatar asked Apr 16 '14 17:04

Abram


2 Answers

You can simply do:

Style.joins(:style_features).where(style_features: { feature_id: [1, 2, 3] })

This query will let Rails deal with the SQL query depending on the DataBase Adapter you defined.

like image 161
MrYoshiji Avatar answered Oct 02 '22 00:10

MrYoshiji


you can try with Style.joins(:style_features).where('style_features.feature_id in (?)', [1, 2, 3])

MrYoshiji Answer is better then me if looking at rails way

like image 37
Nitin Jain Avatar answered Oct 01 '22 23:10

Nitin Jain