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

extend with custom scope #546

Closed
equivalentideas opened this issue Jun 8, 2015 · 5 comments
Closed

extend with custom scope #546

equivalentideas opened this issue Jun 8, 2015 · 5 comments

Comments

@equivalentideas
Copy link

Thanks for the amazing gem ;)

I'm trying the add a new scope to Version to return all the versions from a specific day. I added an initializer paper_trail.rb with:

# the following line is required for PaperTrail >= 4.0.0 with Rails
PaperTrail::Rails::Engine.eager_load!

module PaperTrail
  class Version < ActiveRecord::Base
    scope :created_on_date, ->(date) { where("created_at::timestamp::date = ?", date) }
  end
end

—but, I'm unable to call this in a view, I get the error 'NoMethodError - undefined method `created_on_date' for #PaperTrail::Version::ActiveRecord_Relation:0x007fc50048c1b0'.

This is more a support question than an issue sorry, but is this the right way to extend the Class?

Thanks for your help

@sergiobayona
Copy link

the versions method returns an ActiveRecord::AssociationRelation. So you could just call versions from a method on the ActiveRecord model that's using PaperTrail. For example:

class Widget < ActiveRecord::Base
  has_paper_trail

  def versions_from(date)
    versions.where(created_at: date)
  end
end

@equivalentideas
Copy link
Author

Thanks for that @sergiobayona . I was actually trying to get all versions created on a day, across all the ActiveRecord models using PaperTrail, rather than on an instance of a single model.

@batter
Copy link
Collaborator

batter commented Jun 21, 2015

This is more a support question than an issue sorry, but is this the right way to extend the Class?

Yes... at least I think that should be more or less how it works..

So, I think you would need to define it as a method in current versions of rails instead of declaring a scope, since scope's are deprecated (or so I thought).

Also, you may be able to leverage some of the existing scope methods on the PaperTrail::VersionConcern, such as subsequent, preceding, and between. Hope that is helpful, let me know if you still can't manage to get it working and I'll see if I can be of more assistance.

@batter
Copy link
Collaborator

batter commented Jun 24, 2015

@equivalentideas - To get all the versions created on a particular day, you could call PaperTrail::Version.between(date.to_date, date.to_date + 1.day) or something to that effect. Is that sufficient for your use case until we figure out how to fix custom scope definitions for the PaperTrail::Version class? (See the discussion on #492)

@equivalentideas
Copy link
Author

That does the job, thank @batter .

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

4 participants