Зависимое от пространства имен модели Rails уничтожение вызывает ошибку mysql неизвестное поле

У меня есть несколько моделей в отдельной папке с именем jira (instance.rb, generic_field.rb и т. д.). Все они находятся в пространстве имен JIRA, например JIRA::Instance ‹ ActiveRecord::Base, JIRA::GenericField ‹ ActiveRecord::Base. Вот две модели:

class JIRA::GenericField < ActiveRecord::Base
  self.table_name = "jira_generic_fields"
  belongs_to :jira_instance, class_name: JIRA::Instance
end


class JIRA::Instance < ActiveRecord::Base
  self.table_name = "jira_instances"
  has_many :jira_generic_fields, dependent: :destroy, class_name: JIRA::GenericField
end

Схема БД для таблиц:

create_table "jira_generic_fields", force: true do |t|
  t.string  "jira_id"
  t.string  "name"
  t.integer "jira_instance_id",      null: false
end

create_table "jira_instances", force: true do |t|
  t.string "jira_link"
  t.string "crowd_link"
end

В моей консоли rails я создаю объект JIRA::Instance, и когда я пытаюсь его уничтожить, я получаю следующее:

ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column 'jira_generic_fields.instance_id' in 'where clause': SELECT `jira_generic_fields`.* FROM `jira_generic_fields`  WHERE `jira_generic_fields`.`instance_id` = 1

Почему ActiveRecord использует jira_generic_fields.instance_id вместо jira_generic_fields.jira_instance_id и как это исправить, сохраняя модели в одном и том же пространстве имен JIRA?


person Dan F.    schedule 20.03.2015    source источник


Ответы (1)


В конце концов, указание внешнего_ключа в моделях решило эту проблему...

class JIRA::GenericField < ActiveRecord::Base
  self.table_name = "jira_generic_fields"
  belongs_to :jira_instance, foreign_key: 'jira_instance_id', class_name: JIRA::Instance
end

class JIRA::Instance < ActiveRecord::Base
  self.table_name = "jira_instances"
  has_many :jira_generic_fields, dependent: :destroy, foreign_key: 'jira_instance_id', class_name: JIRA::GenericField
end

Мне это не очень нравится, но пока придется.

person Dan F.    schedule 24.03.2015