Tôi đã sử dụng số source code for railscasts # 141 của Ryan Bates để tạo giỏ hàng đơn giản. Trong một trong những cuộc di cư, ông liệt kêt.belongs_to trong di chuyển
class CreateProducts < ActiveRecord::Migration
def self.up
create_table :products do |t|
t.belongs_to :category
t.string :name
t.decimal :price
t.text :description
t.timestamps
end
end
def self.down
drop_table :products
end
end
Dưới đây là mô hình sản phẩm:
class Product < ActiveRecord::Base
belongs_to :category
end
dòng t.belongs_to :category
là gì? Đó có phải là bí danh cho t.integer category_id
không?
Hầu. 't.references' bây giờ cũng thêm ràng buộc khóa ngoài. https://apidock.com/rails/ActiveRecord/ConnectionAdapters/Table/references –