1

i've got this table:

create_table "packages", force: :cascade do |t|
  t.string   "sender_type"
  t.integer  "sender_account_id"
  t.integer  "sender_user_id"
  t.string   "address"
  t.text     "type"
  t.datetime "created_at",        null: false
  t.datetime "updated_at",        null: false
  t.string   "recipients",                     array: true
end

the recipient array takes the model type and id, separated by a comma, so like ["Manufacturer,4", "Retail,6"].

in my where query, i'm looking to find packages that include any recipients with the word "Manufacturer":

Package.where("sender_type = ? and recipients.include = ?", "school", regex).

just wondering if i'm anywhere close with this

2 Answers 2

0

If the string that you're searching for is always in the first place in your array, you could access it like that:

Package.where("sender_type = ? and recipients[0] LIKE = ?", "school", "Manufacturer").

Not tested so you may need to adapt.

Sign up to request clarification or add additional context in comments.

1 Comment

much appreciated! i ended up ditching the entire concept
0

One of the ways is to convert recepients to text:

Package.where("sender_type = ? and recipients::text LIKE ?", "school", regex)

Or you can use unnest function to conver an array to set of rows. Check this answer: https://stackoverflow.com/a/7222285/7362128

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.