After getting ransack result like this in a controller:
@q = SomeModel.ransack(q)
I would like to query one more time based on the ransack result(@q
).
SELECT * FROM ransack_result WHERE some_field IN ('a', 'b', 'c')
Is there any way to apply additional query?
For more background information:
The some_field
is encrypted with ActiverRecord::Encryption
in Rails7. When I try to include the WHERE some_field IN ('a', 'b', 'c')
by using q[:some_field_in] = ['a', 'b', 'c']
, each of 'a'
, 'b'
, 'c'
is encrypted.
I'm in a transition period which some_field
includes both encrypted and unencrypted value, so I want to query both encrypted & unencrypted data while keeping the other conditions.
ransack does not support encrypted attribute right now, so this is why I cannot use *_in
matcher provided in ransack. :'(
OR... it would be nice if I could get the sql query generated by ransack in rails controller- then I will be able to append the above query!
To perform an additional query based on the result obtained from Ransack, you can extract the SQL generated by Ransack and then "manually" add your custom query conditions:
@q = SomeModel.ransack(q)
# obtain generated SQL by Ransack
original_query = @q.result.arel.to_sql
# your manual modifications
custom_condition = "some_field IN ('a', 'b', 'c')"
custom_query = "#{original_query} AND #{custom_condition}"
# perform the SQL query
SomeModel.find_by_sql(custom_query)