如何在RoR中显示已确认的邀请?

时间:2015-08-16 18:15:02

标签: ruby-on-rails ruby ruby-on-rails-4

我有模型Invintation和模型公司,只有在确认发送消息邀请后,公司才能相互发送消息。

Invintation.rb

class Invintation < ActiveRecord::Base
  belongs_to :recipient, class_name: 'Company', foreign_key: 'recipient_id'
  belongs_to :sender, class_name: 'Company', foreign_key: 'sender_id'
  belongs_to :author, class_name: 'User', foreign_key: 'author_id'
end

Company.rb

class Company < ActiveRecord::Base
  has_many :users_companies
  has_many :users, through: :users_companies
  has_many :sent_messages, class_name: 'Message', foreign_key: 'sender_id'
  has_many :incoming_messages, class_name: 'Message', foreign_key: 'recipient_id'
  has_many :sent_invitations, class_name: 'Invintation', foreign_key: 'sender_id'
  has_many :invitation_recipients, through: :sent_invitations, source: :recipient
  has_many :incoming_invitations, class_name: 'Invintation', foreign_key: 'recipient_id'
  has_many :invitation_senders, through: :incoming_invitations, source: :sender
end

在表格上我想要展示确认邀请的公司

<%= form_for([@company, @message]) do |f| %>
  <%= f.text_field :subject, placeholder: "тема" %>
  <%= f.collection_select :category_id, Category.all, :id, :name, class: "form-control" %>
  <%= f.collection_select :recipient_id, @recipients, :id, :name, class: "form-control" %>
  <%= f.submit %>
<% end %>

所以我添加到控制器这段代码

class MessagesController < ApplicationController

  def new
    @company = Company.find(params[:company_id])
    @message = @company.sent_messages.new
    @recipients = @company.invitation_recipients.where(confirm: true)
  end

  def create
    @company = Company.find(params[:company_id])
    @message = @company.messages.build(mess_params)
    @message.author_id = current_user
    @message.sender_id = @company.id
    if @message.save
      flash[:success] = "Документы успешно отправлены"
      redirect_to inbox_path(@company)
    end
  end
end

但是我不知道我怎么能在@recipients中只显示确认的公司,我哪里出错?

1 个答案:

答案 0 :(得分:0)

我不确定这是否是最有效的方式,但我会有一个邀请模型和链接公司的模型。当公司邀请其他公司时,会创建邀请。当另一家公司接受邀请时,销毁邀请并创建公司之间的链接。然后只向公司展示接收来自关联公司的消息的选项。