错误的属性值传递给表

时间:2014-08-08 15:44:08

标签: ruby-on-rails

我刚刚在我的数据库中注意到,收据表中没有显示recipient_id列的正确receiver_id。我正在使用Mailboxer gem在Questions,Conversations和Receipts表之间工作。

例如,这是问题:

问题表中的新条目显示为

  

id 552,sender_id 1和recipient_id 2。

然后在收据表中创建2个新的托管,该表与刚刚创建的问题条目相关联(默认情况下创建2个托管,一个用于收件人,另一个用于发件人)。

的详细信息
  

第一个条目是id 547和receiver_id 552。

     

第二个条目id为548,receiver_id为1。

正如您在第一个条目中看到的那样,receiver_id正在从问题表id中复制。它应该转移问题表recipient_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"

日志:

Started POST "/questions" for 127.0.0.1 at 2014-08-08 17:00:12 -0400
Processing by QuestionsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"nYOcY69oD6WcfNGEJcKGpyupA0CNbYnAJz0SQ+dKtEk=", "question"=>{"question"=>"Stack Overflow can you help me out?", "sender_id"=>"3", "recipient_id"=>"2"}, "commit"=>"Ask Question"}
  User Load (0.3ms)  SELECT `users`.* FROM `users` WHERE `users`.`auth_token` = 'HkI7Lm4fJYHHf5LHEcMtvA' LIMIT 1
   (0.2ms)  BEGIN
  SQL (0.3ms)  INSERT INTO `questions` (`created_at`, `question`, `recipient_id`, `sender_id`, `updated_at`) VALUES ('2014-08-08 21:00:12', 'Stack Overflow can you help me out?', 2, 3, '2014-08-08 21:00:12')
   (0.4ms)  COMMIT
  User Load (0.4ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 3 ORDER BY `users`.`id` ASC LIMIT 1
   (0.1ms)  BEGIN
  SQL (0.3ms)  INSERT INTO `conversations` (`created_at`, `subject`, `updated_at`) VALUES ('2014-08-08 21:00:12', 'You have a question from 3', '2014-08-08 21:00:12')
  SQL (0.3ms)  INSERT INTO `notifications` (`attachment`, `body`, `conversation_id`, `created_at`, `sender_id`, `sender_type`, `subject`, `type`, `updated_at`) VALUES (NULL, 'Stack Overflow can you help me out?', 419, '2014-08-08 21:00:12', 3, 'User', 'You have a question from 3', 'Message', '2014-08-08 21:00:12')
  SQL (0.3ms)  INSERT INTO `receipts` (`created_at`, `mailbox_type`, `notification_id`, `receiver_id`, `receiver_type`, `updated_at`) VALUES ('2014-08-08 21:00:12', 'inbox', 459, 577, 'Question', '2014-08-08 21:00:12')
   (0.4ms)  COMMIT
   (0.1ms)  BEGIN
  SQL (0.4ms)  INSERT INTO `receipts` (`created_at`, `is_read`, `mailbox_type`, `notification_id`, `receiver_id`, `receiver_type`, `updated_at`) VALUES ('2014-08-08 21:00:12', 1, 'sentbox', 459, 3, 'User', '2014-08-08 21:00:12')
   (0.4ms)  COMMIT
   (0.1ms)  BEGIN
   (0.1ms)  COMMIT
Redirected to http://localhost:3000/users/user
Completed 302 Found in 562ms (ActiveRecord: 62.3ms)

1 个答案:

答案 0 :(得分:1)

当您在控制器中呼叫send_message时,您会将@question作为收件人传递。这意味着第一张收据确实使用question_id作为receiver_id构建。但根据我的理解,您希望问题的收件人被发送消息。只需将您的send_message电话替换为:

current_user.send_message(@question.recipient, @question.question, "You have a question from #{@question.sender_id}") 

幕后发生了什么

来自邮箱的收据表可被视为已发送通知的证明。在您的模型中,添加

acts_as_messageable

您包含邮箱文档中描述的Messageable模块,包括以下send_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方法。以下是该方法的有趣内容,摘自邮箱文档:

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

正如您所见,temp_receipts = recipients.map { |r| build_receipt(r, 'inbox') }为每封邮件收件人构建收据。

第二个有趣的行sender_receipt = build_receipt(sender, 'sentbox', true)发件人建立收据。在您的情况下,current_user

希望它有所帮助。