Ruby on rails 通过activerecord导入gem导入后,所有Rails活动记录外键均为零

Ruby on rails 通过activerecord导入gem导入后,所有Rails活动记录外键均为零,ruby-on-rails,postgresql,activerecord,associations,activerecord-import,Ruby On Rails,Postgresql,Activerecord,Associations,Activerecord Import,Ruby 2.3.0、Rails 4.2.4、PostgreSQL 9.5 更新:在下面添加了activerecord导入代码 有人知道如何保持这些关联,以便在另一个视图中引用模型的表属性吗?类似于另一个Q&A(),我有投资者、公司和交易 我尝试过如下的关联(有很多…到…),但我无法让ActiveRecord识别3个模型和表之间的连接。为数据库设定种子: 数据进入这些表的方式是通过具有3列的csv文件。我使用roo-xls将每个元素提取到一个数组中 基于Myactiverecord导入gem的代

Ruby 2.3.0、Rails 4.2.4、PostgreSQL 9.5

更新:在下面添加了activerecord导入代码

有人知道如何保持这些关联,以便在另一个视图中引用模型的表属性吗?类似于另一个Q&A(),我有投资者、公司和交易

我尝试过如下的关联(
有很多…到…
),但我无法让ActiveRecord识别3个模型和表之间的连接。为数据库设定种子:

数据进入这些表的方式是通过具有3列的csv文件。我使用roo-xls将每个元素提取到一个数组中

基于Myactiverecord导入gem的代码(每个*值是一个由1000个数组组成的数组):

导入工作正常,但当我检查
交易
表时,在执行activerecord导入
.Import
后,公司id和投资者id均为零。我当然希望它们包含
公司
投资者
模型记录的外键

下面是我的型号

Class Company < ActiveRecord::Base
  has_many :investors, 
           :through => :transactions
  has_many :transactions
end

Class Investor < ActiveRecord::Base
  has_many :companies, 
           :through => :transactions
  has_many :transactions
end

Class Transaction < ActiveRecord::Base
  belongs_to :company
  belongs_to :investor
end

嗯。只是想知道,使用source、source\u type而不是默认使用company\u id和investor\u id是否有充分的理由?绝对没有充分的理由…在阅读了许多绑定表的方法(并尝试了所有方法)之后,我现在可能有了一组复杂的关联解决方案。一个更高层次的问题是,我不知道如何让transactions表具有
公司名称
==公司表
名称
(即,为什么表不能通过共享和唯一字符串“对话”?)。我想这就是:source的作用,所以我尝试了它(没有用)——我想:source&:source类型可能是“查找”字符串
company\u name
,将其与companys
name
属性相匹配,并在事务表上生成
company\u id
外键。@Ho Man,我已经解决了这个问题,并删除了
源代码
源代码类型
。我还是搞不懂。事实上,他们的关系看起来是对的。我想你是在跟踪批量进口?也许您可以在rails控制台中尝试它(仅使用1条语句),看看它是否创建了它。你错过了递归:真的吗?@Ho-man…啊,我的activerecord导入策略可能是个问题。我再次阅读了示例/维基,但仍然不知道该怎么办。我在上面更新了它(参见activerecord导入代码段)
Class Company < ActiveRecord::Base
  has_many :investors, 
           :through => :transactions
  has_many :transactions
end

Class Investor < ActiveRecord::Base
  has_many :companies, 
           :through => :transactions
  has_many :transactions
end

Class Transaction < ActiveRecord::Base
  belongs_to :company
  belongs_to :investor
end
class CreatePositions < ActiveRecord::Migration
  def change
    create_table :positions do |t|
      t.string :investor_name
      t.string :company_name
      t.string :percent_owned
      t.belongs_to :company, index: true
      t.belongs_to :manager, index: true
      t.timestamps null: false
    end
  end
end
ActiveRecord::Schema.define(version: 20160128224843) do

  create_table "companies", force: :cascade do |t|
    t.string   "name"
    t.string   "description"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "investors", force: :cascade do |t|
    t.string   "name"
    t.string   "description"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "transactions", force: :cascade do |t|
    t.string   "investor_name"
    t.string   "company_name"
    t.float    "percent_owned"
    t.integer  "investor_id"
    t.integer  "company_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  add_index "transactions", ["investor_id"], name: "index_transactions_on_investor_id", using: :btree
  add_index "transactions", ["company_id"], name: "index_transactions_on_company_id", using: :btree