Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails 4 x paper_trail:使用嵌套资源按项目id筛选版本_Ruby On Rails_Ruby On Rails 4_Activerecord_Nested Resources_Paper Trail Gem - Fatal编程技术网

Ruby on rails Rails 4 x paper_trail:使用嵌套资源按项目id筛选版本

Ruby on rails Rails 4 x paper_trail:使用嵌套资源按项目id筛选版本,ruby-on-rails,ruby-on-rails-4,activerecord,nested-resources,paper-trail-gem,Ruby On Rails,Ruby On Rails 4,Activerecord,Nested Resources,Paper Trail Gem,在我的Rails 4应用程序中,我有以下型号: 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 e

在我的Rails 4应用程序中,我有以下型号:

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
从上面的代码可以推断,问题是数据库中的所有版本(至少最后10个版本)都会被拉出来,不管它们与哪个帖子相关(最重要的是,这个
帖子
是否属于
当前用户
日历

然而,我们想要的是分配给
@versions

  • 所有与之相关的版本
  • 所有来自
  • 所有属于您的日历
  • @使用者
我正在考虑创建一个包含所有相关
post
s
id
s的数组,并将其存储到
@posts
中,然后存储在
@versions
中所有版本
,其中
id
包含在
@posts
数组中

但这似乎很沉重,与Rails的做事方式不一致


您知道我应该如何继续吗?

您可以利用PaperTrail(
项目类型和
项目id
)提供的对象属性:


谢谢你的回答。这部分起作用。好处:它会过滤帖子,并且只显示属于当前用户日历的帖子的版本。剩下的问题是:一旦我们销毁了一篇文章,与此文章对应的所有版本也将消失。“你知道如何改进吗?”ThibaudClement说“部分有效”是不对的。我的回答完全解决了你问题的原始范围,新的澄清超出了原始问题的范围。请用新问题创建新问题。还考虑接受答案,因为它解决了过滤问题足够公平。我在这里提出了一个新问题:
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
PaperTrail::Version
  .where(item_type: 'Post', item_id: Post.where(calendar_id: @calendars.ids))
  .order('id DESC')
  .limit(10)