Created
February 17, 2017 22:40
-
-
Save cpkenn09y/b91266a9df7a9a382795b305cf91a46d to your computer and use it in GitHub Desktop.
where_by_ordered_ids
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Use ActiveRecord to find records based on an array of ids | |
# works with postgresql | |
scope :where_by_ordered_ids, ->(ids) { | |
order = sanitize_sql_array( | |
["position((',' || id::text || ',') in ?)", ids.join(',') + ','] | |
) | |
where(:id => ids).order(order) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment