繁体   English   中英

Rails:将参数哈希传递给模型

[英]Rails: passing params hash to model

我有一个用户对用户消息传递系统。 我正在尝试将用户ID数组传递给ConversationUser (联接表)模型,该模型随后将根据每个user.id创建多个conversation_users ConversationUser中的两个字段是conversation_iduser_id 我可以初始化一个对话用户,因为新的conversation_id会传递给模型,但是由于某种原因,用户id的哈希值并没有进入我的模型。 Validation failed: User can't be blank

我的对话/新视图,用于捕获user_id:

<%= check_box_tag "conversation_user[recipient][]", user.id %> <%= user.name %><br />

我知道这是可行的,因为我收到的部分参数是:

"conversation_user"=>{"recipient"=>["9", "10"]}

我的Rails 4控制器和强大参数的要点:

class ConversationsController < ApplicationController
  def new
    @user = User.find(params[:user_id])
    @conversation = @user.conversation_users.build
    @conversation.build_conversation.messages.build
  end

  def create
    @conv = Conversation.create!
    @conversation = @conv.conversation_users.create!(conversation_user_params)
  end

  def conversation_user_params
    params.require(:conversation_user).permit(recipient: [])
  end

我的ConversationUser模型的要点:

class ConversationUser < ActiveRecord::Base
  attr_accessor :recipient

  before_create :acquire_conversation

  validates :user_id, :conversation_id, presence: true 

  def acquire_conversation
    unless recipient.blank?
      recipient.each do |u|
        ConversationUser.create(user_id: u, conversation: conversation)
      end
    end
  end
end

我认为问题出在控制器的conversation_user_params 但是它也可能在模型的before_create方法中。 我已经尝试解决这一问题了一天,进行了大量调试却没有成功。 如果有人可以提供帮助,我先谢谢您。

问题出在模型中。 在创建ConversationUser之前会调用before_create回调。 让我们将此创建的ConversationUser命名为CURRENT 因此,在创建CURRENT ConversationUser之前,您需要遍历收件人ID并为每个ID创建一个ConversationUser ConversationUser是你们等创建这里不是CURRENT ConversationUser 在执行回调之后(创建其他ConversationUser之后),将保存CURRENT ConversationUser 但在这种情况下, CURRENT ConversationUser不知道至极User属于,因为你传递user_id参数ConversationUser是你们等在创建before_create回调,但你不把它传递给CURRENT ConversationUser创建(原时候,当它create!方法被执行)。

要解决此问题,您可以覆盖原始create! 方法或根本不使用它来通过收件人ID创建ConversationUser 向您的Conversation模型添加一个新方法(例如create_conversation_users ):

在控制器中:

def create
  @conv = Conversation.create!
  @conversation = @conv.create_conversation_users!(conversation_user_params[:recipient])
end

在模型中:

class Conversation
  def create_conversation_users!(recipient_ids)
    return if recipient_ids.blank?

    recipient_ids.each do |recipient_id|
      conversation_users.create!(user_id: recipient_id, conversation: self)
    end
  end
end

您还应该更新ConversationUser模型:

class ConversationUser < ActiveRecord::Base
  validates :user_id, :conversation_id, presence: true 
end

错误在ConversationUser 在运行验证之后,在数据库BUT中创建记录之前运行before_create回调。 要解决您的问题,您可以做一些事情。 Chumakoff回答了其中之一。 这是您可以使用的另一个选项。

删除的所有代码里面ConversationUser并改变conversation_user_params

def conversation_user_params
  params[:conversation_user][recipient].map do |recipient|
    { user_id: recipient }
  end
end

发生的情况是您传递了{ user_id: 1 }数组来create! 这与调用多个create!({ user_id: 1 })

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM