2

In my Rails 4 app, I have the following models:

class User < ActiveRecord::Base
  has_many :administrations, dependent: :destroy
  has_many :calendars, through: :administrations
end

class Administration < ActiveRecord::Base
  belongs_to :user
  belongs_to :calendar
end

class Calendar < ActiveRecord::Base
  has_many :administrations, dependent: :destroy
  has_many :users, through: :administrations
end

class Post < ActiveRecord::Base
    belongs_to :calendar
end

I installed the paper_trail gem to track changes on my post model as explained in the documentation and it works like a charm.

Versions are displayed in the Calendars#Index view, which serves as a dashboard to the user.

Here is how my CalendarsController is setup:

def index
  @user = current_user
  @calendars = @user.calendars.all
  @comments = @user.calendar_comments.order("created_at DESC").limit(20)
  @versions = PaperTrail::Version.order('id DESC').limit(10)
end

As you may infer from the above code, the problem is that ALL versions (at least the last 10 versions) from the database are pulled, regardless of which post they are related to (and most importantly, whether this post belong_to a calendar that belong_to the current_user, or not).

However, what we want is to assign to @versions:

  • All the versions related to
  • All the posts from
  • All the calendars that belong to
  • @user

I am thinking of creating an array of all the relevant posts ids and store it into @posts and then store in @versions all the versions where the id is included in the @posts array.

But this seems heavy and not consistent with the Rails way of doing things.

Any idea about how I should proceed?

Thibaud Clement
  • 6,607
  • 10
  • 50
  • 103

1 Answers1

2

You can make use of object's attributes, provided by PaperTrail (item_type and item_id):

PaperTrail::Version
  .where(item_type: 'Post', item_id: Post.where(calendar_id: @calendars.ids))
  .order('id DESC')
  .limit(10)
Andrey Deineko
  • 51,333
  • 10
  • 112
  • 145
  • Thanks for your answer. This partially works. The good: it does filter posts and only displays versions for posts that belong to a calendar that belong to the current user. The remaining issue: as soon as we destroy a post, all corresponding versions to this post disappear too. Any idea how to improve on that? – Thibaud Clement Nov 15 '15 at 02:18
  • @ThibaudClement I don't think it is right to say that it is "partially works". My answer is fully solved the original scope of your question, and new clarification is out of the original question's scope. Please, create new question with your new problem. Also consider accepting the answer since it solved the filtering issue – Andrey Deineko Nov 15 '15 at 09:43
  • Fair enough. I created a new question here: http://stackoverflow.com/questions/33722350/rails-4-x-paper-trail-keep-track-of-versions-after-item-is-destroyed – Thibaud Clement Nov 15 '15 at 17:04