如何在ActiveRecord 3中预加载动态关联

时间:2014-07-14 12:39:35

标签: ruby-on-rails-3 activerecord

是否可以像这样优化参数化案例:

class Post
  belongs_to :author
  has_many :comments
end

class Comment
  belongs_to :author
  belongs_to :post
  scope :from, -> (author) { where(author: author) }
end

class Author
  has_many :posts
  has_many :comments
end

author = Author.find(42)
author.posts.each do |post|
  if post.comments.from(author).any?
    puts post.inspect
  end
end

0 个答案:

没有答案