代码之家  ›  专栏  ›  技术社区  ›  martins

在对邮件进行分页时,是否将“分页”删除组中的邮件

  •  0
  • martins  · 技术社区  · 6 年前

    我正在使用will-paginate对组内的帖子进行分页。 一个组可能有许多帖子,我不想同时显示所有帖子。 出于某种奇怪的原因,我注意到当我这样做的时候,数据库中有很多帖子被删除了。 @group.posts = paginated_posts 如*组\控制器*中所示。

    我不在@group上保存,为什么要删除这些帖子?

    先测试

     69     it "Pagination – get one group and its posts" do
     70       10.times { Fabricate(:post, group: @group, user: @user) }
     71       puts "POST COUNT BEFORE #{@group.posts.count}"
     72       # byebug
     73       get group_path(@group, posts_per_page: 3)
     74       puts "POST COUNT AFTER #{@group.posts.count}"
     75       expect(response).to have_http_status(200)
     76       expect(@group).to be_present
     77     end
    

    测试输出

    Groups
      GET /group/:group_id?posts_per_page=10&posts_page=2
    POST COUNT BEFORE 12
    POST COUNT AFTER 3
    

    组控制器.rb

      9   def show
     10     paginated_posts = @group.posts.paginate(
     11       page: params[:posts_page],
     12       per_page: params[:posts_per_page] || 100000,
     13     )
     14     @group.posts = paginated_posts
     15     render json: @group
     16   end
    

    原木

      (0.3ms)  RELEASE SAVEPOINT active_record_1
    Started GET "/groups/33?posts_per_page=3" for 127.0.0.1 at 2018-09-18 12:52:13 +0000
    Processing by GroupsController#show as HTML
      Parameters: {"posts_per_page"=>"3", "id"=>"33"}
      User Load (2.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", "78913bd2-4c72-4c73-ba75-421e154c830b"], ["LIMIT", 1]]
      Group Load (1.4ms)  SELECT  "groups".* FROM "groups" WHERE "groups"."id" = $1 LIMIT $2  [["id", 33], ["LIMIT", 1]]
      Post Load (2.2ms)  SELECT  "posts".* FROM "posts" WHERE "posts"."group_id" = $1 LIMIT $2 OFFSET $3  [["group_id", 33], ["LIMIT", 3], ["OFFSET", 0]]
      Post Load (0.3ms)  SELECT "posts".* FROM "posts" WHERE "posts"."group_id" = $1  [["group_id", 33]]
       (0.4ms)  SAVEPOINT active_record_1
      Comment Load (0.5ms)  SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = $1  [["post_id", 156]]
      PostImage Load (0.3ms)  SELECT "post_images".* FROM "post_images" WHERE "post_images"."post_id" = $1  [["post_id", 156]]
      Post Destroy (0.3ms)  DELETE FROM "posts" WHERE "posts"."id" = $1  [["id", 156]]
      Comment Load (0.3ms)  SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = $1  [["post_id", 157]]
      PostImage Load (0.3ms)  SELECT "post_images".* FROM "post_images" WHERE "post_images"."post_id" = $1  [["post_id", 157]]
      Post Destroy (0.3ms)  DELETE FROM "posts" WHERE "posts"."id" = $1  [["id", 157]]
      Comment Load (0.3ms)  SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = $1  [["post_id", 158]]
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   SteveTurczyn    6 年前

    @group.posts = ... 不会像你想象的那样工作。如果您有一对多关系,并且为现有的 group 对象,记录集 去除 这个 group.id 立即使用外键。不需要保存。

    所以当你这样做的时候

    @group.posts = paginated_posts
    

    分页后的职位分配给协会 关联中的所有其他记录都将从关联中删除。立刻。它们不一定被删除,但不再属于 @post

    你可能想改变你的 #to_json …也许加

    attr_accessor :page_of_posts
    

    然后在你的控制器里

    @group.page_of_posts = paginated_posts
    

    并确保 page_of_posts 是JSON中呈现的内容。其他人可能会提出更好的方法。