Rails关联多个:has_many

时间:2015-06-18 18:09:28

标签: ruby-on-rails ruby

我是Rails的新手并试图理解关联实体。

我现在有三个实体:用户,公司和产品线。

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

我已完成迁移程序以关联它们,但是当我点击Rails Admin上的公司时,我得到了这个:

SQLite3 :: SQLException:没有这样的列:productlines.company_id:SELECT“productlines”。* FROM“productlines”WHERE“productlines”。“company_id”=? 提取的来源(第91行):
# def准备sql   stmt = SQLite3 :: Statement.new(self,sql)   除非block_given?

,否则返回stmt

开始

编辑:这是我的迁移:

class AddProductlineIdToCompanies < ActiveRecord::Migration
  def change
    add_column :companies, :productline_id, :integer
    add_index :companies, :productline_id
  end
end

1 个答案:

答案 0 :(得分:0)

您应该将company_id添加到productlines而不是product_line_id添加到companies ...

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