首页 > 解决方案 > 我不确定我的查询语句有什么不正确的地方

问题描述

这是我写的查询语句。我正在尝试获取并打印所有帖子的价值requestor == current_user.id

@posts = Post.joins(user: :follows).where(follows: { requestor: current_user.id }).order(created_at: :desc)

目前,此查询语句仅打印出 的帖子current.id并将每个帖子乘以列出的请求者数量。

requestor:follows包含 a 的表的一部分user.id。我将在下面发布以下内容和发布表格。用户的表使用基本设计。

# Table name: follows
#
#  id         :bigint           not null, primary key
#  following  :bigint
#  requestor  :bigint
#  created_at :datetime         not null
#  updated_at :datetime         not null
#  user_id    :bigint
#
# Indexes
#
#  index_follows_on_user_id  (user_id)
#
# Foreign Keys
#
#  fk_rails_...  (user_id => users.id)
#
# Table name: posts
#
#  id         :bigint           not null, primary key
#  attachment :string
#  content    :text
#  created_at :datetime         not null
#  updated_at :datetime         not null
#  user_id    :bigint
#
# Indexes
#
#  index_posts_on_user_id  (user_id)
#
# Foreign Keys
#
#  fk_rails_...  (user_id => users.id)
#

任何帮助将不胜感激!

在此处输入图像描述

这是视图循环。

<div class="table-responsive">
  <table class="table table-striped table-bordered table-hover">
    <thead>
      <tr>
    

            <th>Attachment</th>
            <th>Content</th>
            <th></th>
            
          <th></th>
        
      </tr>
    </thead>

    <tbody>
      <% @posts.each do |post| %>
        <%= content_tag :tr, id: dom_id(post), class: dom_class(post) do %>
          

                      <td><%= post.attachment %></td>
                      <td><%= post.content %></td>
          
          
            <td><%= link_to 'Edit', edit_post_path(post) %></td>

          
        <% end %>
      <% end %>
    </tbody>
  </table>
</div>

标签: ruby-on-railspostgresqlruby-on-rails-3ruby-on-rails-4ruby-on-rails-5

解决方案


推荐阅读