Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Rails where query should see value is an enum and convert a string #17226
Suppose you have this situation:
Well, that currently doesn't work. You have to do this:
That is so that "enum_value" is converted to the proper integer value.
Even worse, the first query currently does not throw an error and converts the string (any string) to an integer value of zero!
Here's an actual console trace showing this:
Notice, zero is used in the first two queries. The third query is correct.
A related issue is this, using the example from the docs:
Should this code give the same result or different:
statuses = Conversation.select(:status).where.not(status: nil).distinct.map(&:status)
as compared to:
statuses = Conversation.distinct.where.not(status: nil).pluck(:status)
In the first case, you get back an Array with 2 strings: =["active",
If the core team deems this worthy, I could try my hand at a PR to implement this.
At the minimum, this seems worthy for the documentation.
@justin808 It would have a performance cost to check for enums automatically and convert the string to the corresponding integer. But rather of doing:
you should use the provided scopes generated along with enums:
which according to the doc is the recommended way of doing that.
For sure the enum related API could be changed to be less permissive but I think it would introduce code difficult to maintain for near no gain in confort for developers.
@Bounga I see little downside and significant upside to only making the change of using
In terms of the principle of least surprise, it's very surprising to have this sort of behavior shown below. Thus, stricter type conversion would save developers meaningful time in terms of debugging queries. The use of zero is especially problematic, as Enum users often use zero to mean the default value, so it's hard to distinguish that zero is incorrect.
If this is desirable, I could take a crack at making this change and submit a PR.
I personally would use
The gem may help some people but I don't think it will get a lot of attention of the community and for sure it will never make it into the core. But if you do need this, implement it as a gem so you can re-use it easily and share it with the community.
About the enhancement of parameter conversion (
Thus, have the enums generate an additional scope per enum attribute.
The problem with the
I personally like seeing the attribute name with the enum value in the queries. Some of my models have many enum values and just seeing something like
A somewhat related issue is having multiple attributes that share enum values. Has the core team ever gave any recommendation for this use case?
I wrote up a detailed article on this topic: Enums and Queries in Rails 4.1, and Understanding Ruby. I'd be happy to create a PR once there is consensus on what direction to take.
However, it's worth considering if all default accessor methods (setters)
would change to throw an exception, unless the cases are restricted to using
Does this mean the following document is invalid?
@yskkin The document you mention should be valid for Rails 5, while
Sorry, I knew that the issue has been closed, but I would like to post my question related to this issue.
Does anyone know how to customize the query based on the filter query string and enum of the record?
Here is my requirement
In the Record, we have this filter as below
Basically, it works properly when using stage filter such as ?filter[stage]=expired&page[limit]=10
However, we have a requirement that we need to customize the filter/query to add another field such as if the stage filter is expired, we have to check the stage column in database as well as another column (e.g. expired_date). Here is the sample output query for the above URI
Also, when returning the response, if any record matched above filter, we want to change the stage field to expired (or 50 as its value), it is because of some row with expired_date < current time might have different stage value. This is an optional requirement
I am new to RoR, so I would like to know your advice. So could you please take a look and let me know your idea
Thank in advance!