在查询中包含来自另一个表的列(rails activerecord)

时间:2019-11-02 14:24:40

标签: ruby-on-rails activerecord

在对话控制器中,我有以下内容:

  def users_with_existing_conversations
    authorize! :users_with_existing_conversations, Conversation

    @users = User.accessible_by(current_ability, :index_conversations)

    @users = @users.where(id: Conversation.select(:sender_id))
                    .or(@users.where(id: Conversation.select(:recipient_id)))

    @users = @users.search(params[:query]) if params[:query].present?


    @users = sort_and_paginate(@users)
    set_meta_tags title: "Existing Conversations", reverse: true
  end

在用户模型内部,我有这个has_many关系:

  has_many :sender_conversations, class_name: 'Conversation', foreign_key: "sender_id", dependent: :destroy
  has_many :recipient_conversations, class_name: 'Conversation', foreign_key: "recipient_id", dependent: :destroy

在控制器模型中,我具有belongs_to关联:

 belongs_to :sender, foreign_key: :sender_id, class_name: 'User'
  belongs_to :recipient, foreign_key: :recipient_id, class_name: 'User'

返回到控制器,@users对象正在呈现给视图。我需要会话表中的另一列last_updated列。

所以基本上我想从@users表中向conversation添加一个键值对

我尝试过类似的事情

@users.each do |user|
user[:latest_conversation] = Conversation.where(sender_id: user.id)
end

产生can't write unknown attribute latest_conversation

我也尝试过进行类似的测试查询

@testUsers = @users.sender_conversations

哪个产生undefined method sender_conversations

如上所示,我的模型中有关联。该文档显示了示例,我认为这会起作用

我认为我有

 <% @users.each do |user| %>
  <tr class="table__row" onclick="window.location.href = '/conversations?user_id=<%= user.id %>'">
    <td><%= user.name %></td>
    <td><%= user.surname %></td>
    <td><%= user.email %></td>
    <td><%= user.company_name.present? ? user.company_name : "N/A" %></td>
    <td><%= user.role.capitalize %></td>
    <td><%= user.created_at.try(:strftime, '%b %d, %Y') %></td>
    <td><%= user.latest_conversation %></td>
    <td class="table__more">
      <%= link_to "Show details", conversations_path(user_id: user.id), class: 'table__row__details button button--tertiary button--tertiary-small' %>
    </td>
  </tr>
  <% end %>

所以我真的很想在用户循环内访问@user.latest_conversation的方法

2 个答案:

答案 0 :(得分:1)

为什么不在User模型上定义方法?

class User < ...

  def latest_conversation
    sender_conversations.last
  end

答案 1 :(得分:1)

app / models / user.rb:

class User < ApplicationRecord
  # ...

  # combines both "sender" and "recipient" conversations
  # you can also move this into a `has_many :conversations` but you'll need 
  # to `unscope`; see @dre-hh answer here https://stackoverflow.com/questions/24642005/rails-association-with-multiple-foreign-keys
  def conversations
    Conversation.where(sender_id: id).or(
      Conversation.where(recipient_id: id)
    )
  end

  ## instead of above, try the commented code below
  ## Untested, but I think this should also work
  # def conversations
  #  sender_conversations.or(recipient_conversations)
  # end

  # get the latest conversation ordered by "last_updated"
  def latest_conversation
    conversations.order(last_updated: :desc).first
  end
end