Skip to content

Instantly share code, notes, and snippets.

@TrevorS
Last active December 14, 2015 00:09
Show Gist options
  • Save TrevorS/4996786 to your computer and use it in GitHub Desktop.
Save TrevorS/4996786 to your computer and use it in GitHub Desktop.
Working with many to many through associations in Rails.
Switch.find(user_input)
.fields
.includes(:operations)
.includes(:operators)
.map { |field| [field.id, field.name, field.operators.map { |operator| [operator.id, operator.name] } ] }
# Returns:
# [[1, "Record Type", [[1, "="]]],
# [2, "Answer DateTime", [[1, "="], [3, ">"], [4, "<"], [5, ">="], [6, "<="]]],
# [3, "Answer Duration", [[1, "="], [3, ">"], [4, "<"], [5, ">="], [6, "<="]]],
# [4, "Calling Number", [[1, "="], [7, "contains"], [8, "starts with"], [9, "ends with"]]],
# [5, "Called Number", [[1, "="], [7, "contains"], [8, "starts with"], [9, "ends with"]]],
# [6, "Dialed Number", [[1, "="], [7, "contains"], [8, "starts with"], [9, "ends with"]]],
# [7, "IMSI", [[1, "="], [7, "contains"], [8, "starts with"], [9, "ends with"]]],
# [8, "IMEI", [[1, "="], [7, "contains"], [8, "starts with"], [9, "ends with"]]],
# [9, "First MCC-MNC", [[1, "="], [7, "contains"], [8, "starts with"], [9, "ends with"]]],
# [10, "First LAC", [[1, "="], [7, "contains"], [8, "starts with"], [9, "ends with"]]],
# [11, "First Cell ID", [[1, "="], [7, "contains"], [8, "starts with"], [9, "ends with"]]],
# [12, "Last MCC-MNC", [[1, "="], [7, "contains"], [8, "starts with"], [9, "ends with"]]],
# [13, "Last LAC", [[1, "="], [7, "contains"], [8, "starts with"], [9, "ends with"]]],
# [14, "Last Cell ID", [[1, "="], [7, "contains"], [8, "starts with"], [9, "ends with"]]],
# [15, "In Circuit Group", [[1, "="], [7, "contains"], [8, "starts with"], [9, "ends with"]]],
# [16, "Out Circuit Group", [[1, "="], [7, "contains"], [8, "starts with"], [9, "ends with"]]],
# [17, "Cause for Termination", [[1, "="], [7, "contains"], [8, "starts with"], [9, "ends with"]]],
# [18, "Call Reference", [[1, "="], [7, "contains"], [8, "starts with"], [9, "ends with"]]],
# [19, "CAMEL Call Reference", [[1, "="], [7, "contains"], [8, "starts with"], [9, "ends with"]]],
# [20, "Supplementary Service", [[1, "="], [7, "contains"], [8, "starts with"], [9, "ends with"]]]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment