【发布时间】:2018-02-20 10:52:23
【问题描述】:
处理与发票和增值税(两种模型)之间的关联的项目。每次我尝试通过 @Invoice.last.vat.amount 之类的控制台获取增值税值时,我都会收到消息 Rails associations - NoMethodError: undefined method
我认为我已经正确完成了迁移,但不知何故我忽略了一些东西:
我的模型:
class Vat < ActiveRecord::Base
belongs_to :invoice
end
class Invoice < ActiveRecord::Base
has_many :vats
belongs_to :client
end
我的迁移:
对于发票
class CreateInvoices < ActiveRecord::Migration
def change
create_table :invoices do |t|
t.datetime :issue_time
t.integer :total
t.integer :vat
t.string :item
t.string :currency
t.references :client, index: true
t.timestamps
end
end
end
对于增值税
class CreateVats < ActiveRecord::Migration
def change
create_table :vats do |t|
t.integer :amount
t.string :name
t.timestamps
end
end
end
以及后来的修改,将增值税添加到发票:
class AddVatToInvoices < ActiveRecord::Migration
def change
add_reference :invoices, :vat, index: true
end
end
【问题讨论】:
标签: ruby-on-rails