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

Have to restart localhost server for each change #526

Closed
jakeprime opened this issue Apr 22, 2015 · 7 comments
Closed

Have to restart localhost server for each change #526

jakeprime opened this issue Apr 22, 2015 · 7 comments
Milestone

Comments

@jakeprime
Copy link

I have a minor issue where I have to restart my WEBrick or Thin server any time I make any changes to a controller of a versioned model. Any change at all triggers this, no matter how insignificant. The error I get is Could not find table 'versions'

Once I restart the server all is fine. It's only a minor thing, but can becomes a bit of a pain when I am working on one of the controllers.

I have multiple versioned models, each with its own custom named versions table. Everything else is working just fine and as expected. If any other information would be useful let me know.

Thanks

@batter
Copy link
Collaborator

batter commented Apr 27, 2015

What does your initializer / custom version classes look like?

@jakeprime
Copy link
Author

I have a few models which have versioning, each including a Versionable concern which looks like this:

module Versionable
  extend ActiveSupport::Concern

  included do
    has_paper_trail class_name: "#{name}Version"

    class << self
      def creators
        creators_hash = {}

        item_ids = pluck(:id)
        events = "#{name}Version".constantize.where('event=?', :create)
        events.each do |event|
          if item_ids.include?(event.item_id)
            creators_hash[event.whodunnit] ||= 0
            creators_hash[event.whodunnit] += 1
          end
        end

        creators_hash
      end
    end

    def author
      if version.nil?
        originator
      else
        version.originator
      end
    end

    def changes_for(column_name)
      changes = "#{self.class.name}Version".constantize.where(item_id: id)
      changes.where("object_changes LIKE '%\n#{column_name}:\n%'")
    end

    def changes_history_for(column_name)
      changes_for(column_name).where.not(event: :create)
    end

    def past_history_for(column_name)
      all_changes = changes_for(column_name)
      if version.nil?
        all_changes
      else
        all_changes.where('id < ?', version.id)
      end
    end

    def last_edited_info_for(column_name)
      change = past_history_for(column_name).last
      if !change.nil? # there is a previous version
        OpenStruct.new({ author: change.whodunnit, updated_at: change.created_at })
      else # use live one
        self.class.find(id)
      end
    end

    def versions_to_display
      versions.reverse.select{|v| v.event == 'update'}
    end
  end

end

There is nothing to do with versioning in the actual model class files other than include Versionable. The line that is causing the error is versions.reverse.select... right at the end there.

@jakeprime
Copy link
Author

I was asking in the hope that it was an issue that had been seen before with a known fix. If that isn't the case I'll see if I can isolate it in a clean project and post that back if I can.

@batter
Copy link
Collaborator

batter commented Apr 30, 2015

Yes, other people have reported issues similar to this. #488, #483... I'm not certain exactly what causes it but I believe it may have to do with the fact that we load the base PaperTrail::Version class in from a Rails::Engine in PaperTrail 4.x. Well I guess I should probably ask, what version of PaperTrail are you using?

@jakeprime
Copy link
Author

I am using:
Papertrail - 4.0.0.beta2
Rails - 4.2.0
Ruby - 2.1.2

I tried creating a new project, with all the same versions, the same custom classes and the same Versionable concern and it did not have the same issue. If I do manage to isolate it though I'll post back.

@jakeprime
Copy link
Author

This fix works for me:
#492 (comment)

@jaredbeck
Copy link
Member

Thanks for the bug report! Closing; the discussion will continue at #492

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