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

Getting last id #235

Open
carina-akaia opened this issue Jun 3, 2018 · 6 comments
Open

Getting last id #235

carina-akaia opened this issue Jun 3, 2018 · 6 comments

Comments

@carina-akaia
Copy link

carina-akaia commented Jun 3, 2018

Is there a method for last id getting?
Something like User.last_id, more low-cost (and low-level) than current User.all.ids[-1]

@pote
Copy link
Contributor

pote commented Jun 3, 2018

The last ID is stored as a separate Redis key at <ModelName>:id.

You could get the last ID with a Redis GET:

Ohm.redis.call("GET", "User:id")

@carina-akaia
Copy link
Author

carina-akaia commented Jun 3, 2018

Thanks for answer, it helps in my case. It seems like undocumented feature.
I think it might be nice if that functional be realized as OHM model method instead of redis call.

@carina-akaia
Copy link
Author

Can you add feature request label?

@soveran
Copy link
Owner

soveran commented Jun 4, 2018

@katecanaveral I just added the label. Not sure if I prefer Model.id or Model.last_id. Or maybe we just need to document how to get that value from Redis. What was your use case? You can leave your comments here, I guess the next version can include an improvement on this area.

@carina-akaia
Copy link
Author

My case is using last user id for next id prediction, i use it in next username.

last_id = Ohm.redis.call('GET', 'User:id').to_i
    User.create(
        name: "user_#{last_id + 1}",
        ...
    )

User.all.ids[-1].to_i is bit laggy, sometimes i was getting username 'user_30',
although last user id was >40 0_o

About method Model.last_id (i guess .id is implicit name), it's very usable and graceful alias for Ohm.redis.call('GET', 'User:id').to_i

@pote
Copy link
Contributor

pote commented Jun 25, 2018

@katecanaveral regardless of the last id feature, I'd want to comment on a possible source of bugs with your approach.

If timed just right, you might get an incorrect name attribute in your users, since another user might be created between you calling GET User:id and creating the new user, which is a difficult bug to reproduce that is sure to cause you some pain if/when it happens.

But also: it doesn't seem like you need to store the name in an Ohm attribute for that model since the information you want is composed of the model id and a static string, a method like the one below might be enough:

class User < Ohm::Model
  def name
    "user_#{ id }"
  end
end

Maybe there are extra considerations that make you actually need a name Ohm attribute, but I'd still suggest an alternative implementation (like setting the name after the model is created, either synchronously or with a background job) to avoid a possible id/name mismatch bug, happy to help if you need any guidance.

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

No branches or pull requests

3 participants