0

Ruby 2.3.0, Rails 4.2.4, PostgreSQL 9.5

UPDATE: added activerecord-import code below.

Does anyone know how to make these associations hold, so that a model's table attributes can be referenced in another view? Similar to another Q&A (Rails has_many through aliasing with source and source_type for multiple types), where I have investors, companies, and transactions.

I've tried associations like the below (has_many ... through ...), but I'm failing to get ActiveRecord to recognize the connection among the 3 models & tables. Seeding the db:

The way data gets into these tables is via a csv file having 3 columns. I use roo-xls to extract each into an array of arrays.

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

icol = [:name]
ccol = [:name]
tcol = [:investor_name, :company_name, :percent_owned]

investor_val = [["i1"],["i2"]] # just showing 2 arrays for 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, but when I check the transactions table, both company_id and investor_id are nil after executing the activerecord-import .import. I of course would like them to contain the foreign keys for the company and investor model records.

My models are 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 for brevity)

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

My schema, where I've added references to the belongs_to (transactions) table.

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
Community
  • 1
  • 1
JHFirestarter
  • 63
  • 1
  • 9
  • mmm. Just wondering, is there a strong reason to use source, source_type as opposed to just defaulting to using company_id and investor_id? – Ho Man Jan 29 '16 at 05:20
  • Definitely no strong reason...after reading many ways to bind the tables (and trying them all), I may have a motley crew of association solutions now. One higher-level thing I can't figure out is how to have the transactions table with a `company_name` == companies table `name` (i.e. why can't the tables "talk" via a shared & unique string?)...I thought that's what :source does, so I tried it (to no avail)--I thought :source & :source_type might "lookup" the string `company_name`, match it to the companies `name` attribute, and generate the `company_id` foreign key on the transactions table. – JHFirestarter Jan 29 '16 at 05:35
  • @Ho Man, I've cleaned up the question and removed the `source` & `source_type`. Still can't figure it out. – JHFirestarter Mar 01 '16 at 01:47
  • The relations look right actually. I suppose you're following https://github.com/zdennis/activerecord-import/wiki for the bulk import? Perhaps you can try it in rails console (with just 1 statement) and see if it creates it. Did you miss out recursive: true? – Ho Man Mar 01 '16 at 02:42
  • @Ho man...ah, my activerecord-import strategy may be the issue. I read the examples / wiki again, and I'm still not sure what to do. I updated that above (see **activerecord-import** snippet) – JHFirestarter Mar 01 '16 at 13:34
  • Looked at activerecord-import gem issues in-depth, and importing models gets me part of the way there...but the foreign keys do not survive duplicate issues. – JHFirestarter Mar 02 '16 at 20:52

0 Answers0