Użyłem Ryana Batesa source code for railscasts # 141 w celu utworzenia prostego koszyka na zakupy. W jednej z wędrówek, wymienia ont.belongs_to w migracji
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
Oto model produktu:
class Product < ActiveRecord::Base
belongs_to :category
end
Co jest linia t.belongs_to :category
? Czy to jest alias dla t.integer category_id
?
Prawie. 't.references' teraz dodaje również ograniczenie klucza obcego. https://apidock.com/rails/ActiveRecord/ConnectionAdapters/Table/references –