Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Helper for queries on Version object fields #377

Closed
JacobEvelyn opened this issue Jun 3, 2014 · 8 comments
Closed

Helper for queries on Version object fields #377

JacobEvelyn opened this issue Jun 3, 2014 · 8 comments
Milestone

Comments

@JacobEvelyn
Copy link
Contributor

Would there be interest in adding a method to the concern that allows queries to be made more safely/easily on the object serialized field?

I'm thinking something like:

VersionClass.where_object(name: "Foo", other_field: 42)

which translates into:

VersionClass.where("object LIKE '%name: Foo\n%'").
  where("object LIKE '%other_field: 42\n%'")

I end up making these queries fairly frequently, and I find them easy to mess up (for instance, forgetting the \n or %s) and I think a method to automate this would be helpful. I'm more than happy to submit a pull request for this change if it's something others would want as well.

@batter
Copy link
Collaborator

batter commented Jun 3, 2014

I like this idea. I'm sure if you have a lot of versions in your table, this search is probably pretty slow, but still a nice feature to have. A PR would be be great, cheers.

@batter batter added this to the 3.0.3 milestone Jun 3, 2014
@bradleypriest
Copy link
Contributor

If you do, can you try and make it storage specific. I'm using the PG json type so the query would actually be different than what you have above.

@JacobEvelyn
Copy link
Contributor Author

Yep, definitely.

@batter
Copy link
Collaborator

batter commented Jun 4, 2014

@JacobEvelyn
Copy link
Contributor Author

Awesome, thanks guys. I'll try to get this out by the end of the week.

@JacobEvelyn
Copy link
Contributor Author

So just to clarify, my understanding is that this should have the ability to perform a string (LIKE) query on Yaml data, a LIKE query on JSON, and native Postgres JSON queries?

@batter
Copy link
Collaborator

batter commented Jun 5, 2014

Yea I think that sounds about right. You can probably first do a check to see if it's using the Postgres JSON, then if it's not, check the serializer in usage by calling PaperTrail.serializer.

@JacobEvelyn
Copy link
Contributor Author

@bradleypriest any help on a native JSON query would be much appreciated. I haven't been able to figure out how to get basic field comparisons working in Postgres 9.3 without resorting to using ->> to perform string comparisons, and being unable to differentiate between a JSON null and an empty string. 9.4 seems like it may offer more support but I wasn't able to install it successfully on my machine.

If you could, SQL examples (for querying JSON on all different JSON data types) would be wonderful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants