Ruby on rails 传递给表的属性值错误

Ruby on rails 传递给表的属性值错误,ruby-on-rails,Ruby On Rails,我刚刚注意到,在我的数据库中,Receipts表中没有为receiver\u id列显示正确的receiver\u id。我正在与Mailboxer gem在问题、对话和收据表之间工作 例如,问题是: 问题表中会出现一个新条目,其中包含 id552、sender\u id1和receiver\u id2 然后在与刚创建的问题条目关联的Receipts表中创建2个新条目(默认情况下创建2个条目,一个用于收件人,另一个用于发件人)。详情为何? 第一个条目是id547和receiver\u id552

我刚刚注意到,在我的数据库中,Receipts表中没有为
receiver\u id
列显示正确的
receiver\u id
。我正在与Mailboxer gem在问题、对话和收据表之间工作

例如,问题是:

问题表中会出现一个新条目,其中包含

id
552、
sender\u id
1和
receiver\u id
2

然后在与刚创建的问题条目关联的Receipts表中创建2个新条目(默认情况下创建2个条目,一个用于收件人,另一个用于发件人)。详情为何?

第一个条目是
id
547和
receiver\u id
552

第二个条目
id
是548和
receiver\u id
1

如您所见,对于第一个条目,
receiver\u id
正在从问题表
id
复制。它应该转移问题表
recipient\u id

我不知道如何解决这个问题

问题:

 def create
      @question = Question.new(params[:question])
      if @question.save
        @message = current_user.send_message(@question, @question.question, "You have a question from #{@question.sender_id}") 
        redirect_to :back, notice: 'Your question was saved successfully. Thanks!'
      else
        render :new, alert: 'Sorry. There was a problem saving your question.'
      end
    end
  end
问题模式:

  acts_as_messageable

  attr_accessible :answer, :question, :sender_id, :recipient_id, :conversation_id
  belongs_to :user

  belongs_to :sender,:class_name => 'User',:foreign_key => 'sender_id'

  belongs_to :recipient,:class_name => 'User',:foreign_key => 'recipient_id'

  belongs_to :message

  belongs_to :conversation
show.html.slim:

center
  .message_div
    = form_for Question.new, class: 'question_form form-horizontal', role: 'form' do |f|
      .form-group
        = f.text_field :question, {:placeholder => 'Please add your question...',class:'form-control'}
      = f.hidden_field :sender_id, :value => current_user.id
      = f.hidden_field :recipient_id, :value => @user.id
      = f.submit 'Ask Question', class: 'btn btn-primary'
schema.rb:

create_table "receipts", force: true do |t|
    t.integer  "receiver_id"
    t.string   "receiver_type"
    t.integer  "notification_id",                            null: false
    t.boolean  "is_read",                    default: false
    t.boolean  "trashed",                    default: false
    t.boolean  "deleted",                    default: false
    t.string   "mailbox_type",    limit: 25
    t.datetime "created_at",                                 null: false
    t.datetime "updated_at",                                 null: false
  end

  add_index "receipts", ["notification_id"], name: "index_receipts_on_notification_id", using: :btree

  add_foreign_key "receipts", "notifications", name: "receipts_on_notification_id"
日志:


当您在控制器中调用
send_message
时,您将作为收件人传递
@question
。这意味着第一张收据确实是用
question\u id
作为
receiver\u id
构建的。但据我所知,你希望问题的接收者收到信息。只需将您的send_message呼叫替换为:

current_user.send_message(@question.recipient, @question.question, "You have a question from #{@question.sender_id}") 
幕后发生了什么

来自mailboxer的收据表可以被视为已发送通知的证据。在模型中,通过添加

acts_as_messageable
您包括邮箱程序文档中描述的
Messageable
模块,包括以下
send\u message
方法:

def send_message(recipients, msg_body, subject, sanitize_text=true, attachment=nil, message_timestamp = Time.now)
  # message = ...
  # message building stuff happening

  message.deliver false, sanitize_text
end
当您从控制器调用该方法时,它会对这个新创建的消息调用deliver方法。下面是从mailboxer文档中提取的该方法的有趣部分:

def deliver(reply = false, should_clean = true)
  # ... some stuff

  #Receiver receipts
  temp_receipts = recipients.map { |r| build_receipt(r, 'inbox') }

  #Sender receipt
  sender_receipt = build_receipt(sender, 'sentbox', true)

  temp_receipts << sender_receipt

  if temp_receipts.all?(&:save!)
    # some other stuff
  end
  sender_receipt
end
def deliver(reply=false,should_clean=true)
# ... 一些东西
#收款人收据
temp|u receives=recipients.map{| r | build|u receive(r,'inbox')}
#寄件人收据
发送方接收=生成接收(发送方“sentbox”,true)

您可以发布Receipt模型和控制器(假设分配的值不正确)。另外,如果您可以将正在传递的参数发布到controller@ob264没有收据模型或控制器。Mailboxer gem没有指示你这样做(查看帖子,我注意到我从未提到过gem)。你为什么要在
create
方法中保存两次这个问题?@egeesoz,我错过了。感谢您提供了大量信息,并解释得很好。我发誓我试过问号。收件人,当我这么做的时候收到了一个错误。我一定是拼错了什么的。尽管如此,它还是起作用了,你让我对Mailboxer有了更多的了解。谢谢
def deliver(reply = false, should_clean = true)
  # ... some stuff

  #Receiver receipts
  temp_receipts = recipients.map { |r| build_receipt(r, 'inbox') }

  #Sender receipt
  sender_receipt = build_receipt(sender, 'sentbox', true)

  temp_receipts << sender_receipt

  if temp_receipts.all?(&:save!)
    # some other stuff
  end
  sender_receipt
end