仅使用特定ID获取数据

时间:2018-03-17 11:02:52

标签: ruby-on-rails jbuilder

我正在使用rails API创建一个项目。现在,我正在获取所有ID的所有数据,但我想获取特定ID的数据。为此,我做了:

路线:

get 'all_club_post' => 'clubs#all_club_post' 

控制器:

def all_club_post
active_posts = []
@posts = []
posts = Post.all.order(id: :desc).pluck(:id)
@hide_posts = current_user.hide_posts.pluck(:post_id)
active_posts = posts - @hide_posts
active_posts.each do |post|
  @posts << Post.find(post)
  end
end 

的JBuilder:

json.posts{
json.array! @posts do |post|
    if post.club_id.present?
        json.id post.id
        json.counter_id post.id
        json.edit_id post.id
        json.user_id post.user_id
        json.club_id post.club_id
        json.post_text post.post_text
        json.created_at post.created_at.strftime('%d %b,%Y %H:%M')
        json.full_name post.user.full_name
        json.likes post.like_posts.count
        json.current_user_liked liked_post(post)

        json.like_posts{
          json.array! post.like_posts do |like_post|
            json.id like_post.id
            json.user_id like_post.user_id
            json.post_id like_post.post_id
            json.full_name like_post.user.full_name
          end
        }
    end
  end
}

现在我得到的结果是:

{
"posts": [
    {
        "id": 22,
        "counter_id": 22,
        "edit_id": 22,
        "user_id": 4,
        "club_id": 3,
        "post_text": "I work in jetsetgo",
        "created_at": "17 Mar,2018 09:06",
        "full_name": "Rahul Wadhwa",
        "likes": 0,
        "current_user_liked": false,
        "like_posts": []
    },
    {
        "id": 21,
        "counter_id": 21,
        "edit_id": 21,
        "user_id": 4,
        "club_id": 1,
        "post_text": "My Name is Amit.",
        "created_at": "17 Mar,2018 07:09",
        "full_name": "Rahul Wadhwa",
        "likes": 0,
        "current_user_liked": false,
        "like_posts": []
    },
    {
        "id": 18,
        "counter_id": 18,
        "edit_id": 18,
        "user_id": 2,
        "club_id": 1,
        "post_text": "HI",
        "created_at": "16 Mar,2018 13:18",
        "full_name": "Ravi Yadav",
        "likes": 0,
        "current_user_liked": false,
        "like_posts": []
    }
  ]
}

但我需要根据俱乐部ID提供数据。就像我在club_id中一样:3&#39; http://localhost:4200/club/3&#39;需要以下数据:

    {
"posts": [
    {
        "id": 22,
        "counter_id": 22,
        "edit_id": 22,
        "user_id": 4,
        "club_id": 3,
        "post_text": "I work in XYZ",
        "created_at": "17 Mar,2018 09:06",
        "full_name": "Rahul Wadhwa",
        "likes": 0,
        "current_user_liked": false,
        "like_posts": []
    }
  ]
}

2 个答案:

答案 0 :(得分:0)

我认为模型本身有一个方法。尝试

Post.find(params[:id])

这应该可以解决问题。尝试挖掘更多Here

答案 1 :(得分:0)

你为什么这样做:

def all_club_post
  active_posts = []
  @posts = []
  posts = Post.all.order(id: :desc).pluck(:id)
  @hide_posts = current_user.hide_posts.pluck(:post_id)
  active_posts = posts - @hide_posts
  active_posts.each do |post|
    @posts << Post.find(post)
  end
  ...
end

这有很多问题,包括(可能最糟糕的)你正在进行N + 1查询:你进行查询以获得posts,然后你对@posts << Post.find(post)进行N次查询。

相反,这可能是一个简单的事情:

def all_club_posts
  @posts = Post.where.not(id: current_user.hide_posts)
  ...
end