0

I have an ActiveRecord query:

Post.where(users: [])

In Rails 5.1.1, this results in:

SELECT `posts`.* FROM `posts` WHERE (users = '[]')

but in Rails 5.2.2, this results in:

SELECT `posts`.* FROM `posts` WHERE 1=0

Does anyone know if this is expected behaviour in Rails 5.2 or a bug?

2
  • 1
    Why not just use a proper join table instead of a serialized array? Thats probably the worst possible design. Commented Dec 6, 2018 at 9:08
  • Are you able to answer the question? Commented Dec 6, 2018 at 9:11

1 Answer 1

1

That is not a bug , 5.2.2 updated for remove SQL Injection problem.

SELECT `posts`.* FROM `posts` WHERE (users = '[]') 

If you observe this query we have a SQL Injection problem , that is if you pass some query instead of value then it will pass . It's too dangerous. You can use below code for to retrieve data based on array objects with where condition.

ModelName.where('users IN (?)', [array of elements] )
Sign up to request clarification or add additional context in comments.

5 Comments

Can you explain more how this is a SQL inject problem? what do you mean by "query instead of value"?
But you can still do Post.where(some_value: some_integer). My question is more about why we can no longer query for serialized arrays.
Follow "netsparker.com/blog/web-security/…" this link . You will get better idea . If you have any doubt then comment me again .
You can use where condition with array like this ' Model.where('users IN (?)', [array of elements] ) '. From this we can grab all records of Model with array objects
So how can I get around this, I can now never use ActiveRecord's #where method when one of the fields is serialized...

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.