0
votes

I'm new to Rails and trying to understand associating entities.

I have three entities right now: Users, Companies, and Productlines.

company.rb:

class Company < ActiveRecord::Base
    has_many :users
    has_many :productlines
end

user.rb:

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  belongs_to :company
end

productline.rb:

class Productline < ActiveRecord::Base
    belongs_to :company
end

I've done the migration scrips to associate them, however when I click into a Company on Rails Admin, I'm getting this:

SQLite3::SQLException: no such column: productlines.company_id: SELECT "productlines".* FROM "productlines" WHERE "productlines"."company_id" = ? Extracted source (around line #91):
# def prepare sql stmt = SQLite3::Statement.new( self, sql ) return stmt unless block_given?

begin

Edit: Here is my migration:

class AddProductlineIdToCompanies < ActiveRecord::Migration
  def change
    add_column :companies, :productline_id, :integer
    add_index :companies, :productline_id
  end
end
1
post your migrations.... the company_id is missing in the productlines table - gabrielhilal
Just edited my question - beaconhill
Did you remember to run rake db:migrate after adding that migration? - David Hoelzer

1 Answers

0
votes

You should add the company_id to productlines and not product_line_id to companies...

class AddCompanyIdToProductlines < ActiveRecord::Migration
  def change
    add_column :productlines, :company_id, :integer
  end
end