创建方法rails上的TypeError(没有将Array隐式转换为String)活动记录

时间:2017-07-29 11:06:15

标签: ruby-on-rails arrays activerecord ruby-on-rails-5

我有以下型号

class Business < ApplicationRecord {
               :id => :integer,
    :business_name => :string,
         :place_id => :string,
          :surburb => :string,
             :type => :text,
       :created_at => :datetime,
       :updated_at => :datetime
}

我正在向我的api create方法发送一个JSON对象,这是我的对象

business_params = {
    :business_name => "Endah Parade",
          :surburb => "Taman Sri Endah",
             :type => [
        [0] "premise",
        [1] "point_of_interest",
        [2] "establishment"
    ],
         :place_id => "ChIJixfh-49KzDERePyk34svbaY"
}

在我的控制器中,我运行以下

business = Business.create!(business_params)

但我收到错误TypeError (no implicit conversion of Array into String)。我的type列定义为此

change_column :businesses, :type, :text, array: true, default: [], using: "(string_to_array(type, ','))"

所以至少我知道我的类型列可以接受数组,但我不知道为什么我会收到此错误。

在我的商业模式中,我添加了以下serialize :type, Array,但它也没有帮助。我犯了同样的错误。

我该如何解决这个问题?

我正在使用Rails 5.1

1 个答案:

答案 0 :(得分:1)

这是因为Rails默认使用type列来实现STI

禁用STI应该会有所帮助:

class Business
  self.inheritance_column = nil
end