Migración Rails: t. referencias con nombre alternativo?


Así que tengo una create_table como esta para Cursos en una Escuela:

create_table :courses do |t|
  t.string :name
  t.references :course
  t.timestamps
end

, Pero quiero hacer referencia a dos otros cursos como:

has_many :transferrable_as # A Course
has_many :same_as          # Another Course

Puedo decir lo siguiente?

t.references :transferrable_as, :as=> :course
Author: Arslan Ali, 2010-05-29

5 answers

Puede hacer todo esto en la definición de migración/columna inicial (al menos actualmente en Rails 5):

t.references :transferable_as, index: true, foreign_key: {to_table: :courses}
t.references :same_as, index: true, foreign_key: {to_table: :courses}
 91
Author: Ryan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-04-17 13:33:31

Puedes hacerlo de esta manera:

create_table :courses do |t|
  t.string :name
  t.references :transferrable_as, references: :courses
  t.references :same_as, references: :courses
  t.timestamps
end

O usando t.belongs_to como alias para t.references

No se puede añadir foreign_key: true a esas dos líneas de referencia. Si desea marcarlas como claves foráneas a nivel de base de datos, debe tener una migración con esto:

add_foreign_key :courses, :courses, column: :transferrable_as_id
add_foreign_key :courses, :courses, column: :same_as_id
 73
Author: Toby 1 Kenobi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-12-03 15:31:05

Creo que este hilo tiene una manera diferente más Rails-ish: Scaffolding ActiveRecord: dos columnas del mismo tipo de datos

En la migración:

T. belongs_to: transferrable_as

T. belongs_to: same_as

 13
Author: themirror,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 12:18:01

Como respuesta añadida a esta pregunta the el Modelo debe tener la siguiente línea para completar la asociación:

    belongs_to :transferrable_as, class_name: "Course"
    belongs_to :same_as, class_name: "Course"
 4
Author: Diego Diaz de Berenguer,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-07-12 22:12:51

No creo que references acepte la opción :as, pero puede crear sus columnas manualmente...

create_table :courses do |t| 
  t.string  :name 
  t.integer :course1_id
  t.integer :course2_id 
  t.timestamps 
end 
 3
Author: Ju Nogueira,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-05-29 14:26:37