default_scope and associations

前端 未结 6 1584
猫巷女王i
猫巷女王i 2020-12-06 04:17

Suppose I have a Post model, and a Comment model. Using a common pattern, Post has_many Comments.

If Comment has a default_scope set:

default_scope w         


        
6条回答
  •  甜味超标
    2020-12-06 05:07

    class Comment
      def post_comments(post_id)
        with_exclusive_scope { find(all, :conditions => {:post_id => post_id}) }
      end
    end
    
    Comment.post_comments(Post.first.id)
    

提交回复
热议问题