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

How can I get the last 100 versions of a model? regardless of the record #240

Closed
parkournick3 opened this issue May 5, 2023 · 2 comments

Comments

@parkournick3
Copy link

is there any method or helper in logidze to do this? something like that

Job.last_versions(100) # output: array of log_datas

Is there any way to do this manually?

@parkournick3
Copy link
Author

def last_version(model)
      model.log_data.data['h'].first do |version_log_data|
        version_number = version_log_data['v']
        version = model.at(version: version_number)
        version.responsible_user_id = version.log_data.responsible_id

        version.log_difference_changes = if version_number > 1
                                           model.at(version: version_number)
                                                .diff_from(version: version_number - 1)['changes']
                                         else
                                           model.at(version: version_number).log_data.data['h'][0]['c']
                                         end
        return version
      end
end

versions = []

@jobs = Job.order(updated_at: :desc).where('jobs.updated_at >= ?', 7.days.ago)

@jobs.each do |job|
      versions.push last_version(job, only_last_version) unless job.log_data.nil?
end

I managed with this code, but only filtering updated_at, in my case it works, but it would be really cool to have this feat <Model>.last_versions(limit = 100)

@palkan
Copy link
Owner

palkan commented May 17, 2023

Hey @parkournick00110010!

Can you please take a look at this PR #238? Looks very similar; maybe, we can incorporate your idea there.

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