I can easily create a sketch or model in Rails with a field that is a link (foreign key) to another model:
rails g model Cat owner:references rails g scaffold Cat owner:references
But I can not do the same for adding a column to the hyphen:
rails g migration AddOwnerToCats owner:references
The above example creates a migration file as follows:
class AddOwnerToCats < ActiveRecord::Migration def change add_column :cats, :owner, :references end end
And when I try to run it using rake db:migrate , I get the following:
SQLite3::SQLException: near "references": syntax error: ALTER TABLE "cats" ADD "owner" references
So, is there a way to add a column that is a link to another model? Or I just need to do:
rails g migration AddOwnerToCats owner_id:integer
And then go to the move and add an index for owner_id ?
source share