postgresql - All Rails active record foreign keys are nil after import via activerecord-import gem -


ruby 2.3.0, rails 4.2.4, postgresql 9.5

update: added activerecord-import code below.

does know how make these associations hold, model's table attributes can referenced in view? similar q&a (rails has_many through aliasing source , source_type multiple types), have investors, companies, , transactions.

i've tried associations below (has_many ... through ...), i'm failing activerecord recognize connection among 3 models & tables. seeding db:

the way data gets these tables via csv file having 3 columns. use roo-xls extract each array of arrays.

my activerecord-import gem-based code (each *_val array of 1000s of arrays):

icol = [:name] ccol = [:name] tcol = [:investor_name, :company_name, :percent_owned]  investor_val = [["i1"],["i2"]] # showing 2 arrays brevity company_val = [["c1"],["c2"]] # "" transaction_val = [["i1","c1","pct1"],["i2","c2","pct2"]] # ""  investor.import icol, investor_val, :validate => false company.import ccol, company_val, :validate => false transaction.import tcol, transaction_val, :validate => false 

import works, when check transactions table, both company_id , investor_id nil after executing activerecord-import .import. of course them contain foreign keys company , investor model records.

my models below.

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 

transactions migration (others left out brevity)

class createpositions < activerecord::migration   def change     create_table :positions |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 

my schema, i've added references belongs_to (transactions) table.

activerecord::schema.define(version: 20160128224843)    create_table "companies", force: :cascade |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 |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 |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 


Comments