从CSV创建两个模型

问题描述 投票:0回答:1

我正在从CSV文件(名称,电子邮件地址,状态)加载信息。从CSV文件中我想创建一个Customer和一个SurveyRequest。我在线跟踪了一堆教程,可以创建客户记录,但是在创建SurveyRequest记录时出错了。所有示例都显示Customer中创建记录的方法,但我对如何使Customer模型创建SurveyRequest感到困惑。我看起来可能与Customer和SurveyRequest模型之间的关系有所了解,但我不确定。

我得到的错误是:

undefined method `survey_request_id' for #<SurveyRequest:0x00007f9197f45378> Did you mean? survey_token_changed?

并且@ survey_request.save行发生错误

以下是Customer.rb模型类Customer <ApplicationRecord

  enum status: { active: 0, inactive: 1, removed: 2 }

  belongs_to :product
  #belongs_to :survey_request
  has_many :survey_requests
  has_secure_token :customer_token

  #the below is for importing customers from a CSV file
  def self.import(file, product_id)
    #begin
      CSV.foreach(file.path, headers: true) do |row|
        customer_hash = row.to_hash
        if customer_hash['is_new'].to_i == 1
          @customer = Customer.new(name: customer_hash['name'], email: customer_hash['email'], product_id: product_id) #to whitelist data for entry so it can be saved
          @customer.status = :active
          @customer.save #if saved ok
          #now that customer is saved, need to create a corresponding SurveyRequest
          @product = Product.where("id = ?", @customer.product_id)
          @survey_request = SurveyRequest.new
          @survey_request.product_id = @product.id
          @survey_request.customer_id = @customer.id
          @survey_request.date_to_send = @product.days_after_for_new_customer.days
          @survey_request.save
          #elsif para is_new = 0 then create existing customer
        elsif customer_hash['is_new'].to_i == 0
          #@customer = Customer.new(customer_hash) #to whitelist data for entry so it can be saved
          @customer = Customer.new(name: customer_hash['name'], email: customer_hash['email'], product_id: product_id)
          @customer.status = :active
          @customer.save #if saved ok
          @product = Product.find(@customer.product_id)
          @survey_request = SurveyRequest.new
          @survey_request.product_id = @product.id
          @survey_request.customer_id = @customer.id
          @survey_request.date_to_send = @product.days_after_for_existing_customer.days
          @survey_request.save
        end
    end
  end
end

这是Survey.rb模型

class SurveyRequest < ApplicationRecord

  enum request_type: { new_customer: 0, existing_customer: 1, new_release: 2, new_customer_reminder: 3, existing_customer_reminder: 4, new_release_reminder: 5 }

  belongs_to :product
  #has_one :customer
  belongs_to :customer
  has_secure_token :survey_token

  attr_accessor :customer_email
  validates :customer_email, :presence => true
  validates :survey_request_id, :presence => true
end

这是customers_controller.rb文件的导入方法

def import
  @product = Product.find(1)
  #confirm that the user uploading the file to the product is the owner
  if @product.user_id = current_user.id
    Customer.import(params[:file], @product.id)
    flash[:success] = "Customers created."
    redirect_to product_customers_path(:product_id => @product.id)
  else
    flash[:danger] = "It does not appear you are the creator of this product. If you believe you are receiving this message in error, please contact customer support."
    redirect_to users_dashboard_path
  end
end
ruby-on-rails import-from-csv
1个回答
0
投票

删除SurveyRequest模型中的行:validates :survey_request_id, :presence => true

ID将始终由数据库自动生成,无需验证。该列也将被称为:id。

© www.soinside.com 2019 - 2024. All rights reserved.