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

Any plans for Model.last and Query method? #3

Open
ChrisZou opened this issue Feb 26, 2018 · 1 comment
Open

Any plans for Model.last and Query method? #3

ChrisZou opened this issue Feb 26, 2018 · 1 comment

Comments

@ChrisZou
Copy link

ChrisZou commented Feb 26, 2018

Hi @meyercm , sorry for coming to complain again. Do you have any plans for generating a last method(order_by: [desc: :inserted_at]) for model, just like ActiveRecord#last. I personally find it very useful.
Another feature I found useful is generating a bunch of methods that returns a Ecto.Query. These methods could be bundled up in a generated Module, like Model.Query.query_method.
For example.
User.Query.where(username: "chris") would return a Query similar to from(User, where: [username: "chris"]). This would simplify lots of boilerplate code when composing query or when using preload.
Sorry for my poor English, I hope I've made myself clear.

@meyercm
Copy link
Owner

meyercm commented Feb 26, 2018

Model.last has been on my to-do list. However, since not all models will have :inserted_at, my plan had been to use order_by: [desc: <pk>], which works for most choices of primary key type, but breaks down for guids. I'm tempted to provide the signature last(by_column \\ :__schema_primary_key__), however, that precludes providing a count, the way it works in ActiveRecord (you wouldn't be able to write User.last(3) like in their documentation ). ActiveRecord solves the problem with their default_scope, which is a feature I never used in Ruby, and so didn't make it in the original BaseModel. I could refactor to include it now, probably as a callback, similar to create_changeset. I'm open to ideas for making last syntax both convenient and consistent with the rest of BaseModel.

I'll have to think some on your Model.Query notion. In my production code, as soon as I need something that BaseModel doesn't provide, I write a method in the model that just uses Ecto directly. However, I could see this being very useful while debugging on the console. What I don't want to do is re-write Ecto.Query; the goal of BaseModel is just to make using Ecto very easy for the most common calls.

It would help if you had some complete examples of how you would like to use it. I know that isn't easy, since the feature doesn't exist yet, but I'm having a hard time weighing the value in the abstract.

Also, your English is great- if you aren't a native speaker, you should be proud.

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

2 participants