在Rails中将英语模型与西班牙语数据库一起使用

时间:2018-07-18 20:03:31

标签: mysql ruby-on-rails activerecord

我刚刚将Rails应用程序链接到一个mysql数据库,该数据库具有西班牙语的表名和列。现在,我通过在self.table_name = "table_name"内设置model.rb解决了西班牙表名的问题。现在,当我想通过联接表调用数据时,出现下一个问题。在这种情况下,我试图调用属于第一个ads的所有category。如下面的屏幕快照所示,当我尝试此操作时,出现此错误。它现在看到adanuncio,这是西班牙语名称。我有点困惑,因为我认为通过在每个模型中执行self.table_name = "table_name",Rails会知道我的意思是哪个表。有人知道这里发生了什么以及如何解决吗?下面查看我有关模型和表格的所有代码。

Error Rails App

广告模型:

class Ad < ApplicationRecord
  self.table_name = "anuncios"
  has_many :ad_copies
  has_many :ad_addresses
  has_many :relationships
  has_many :magazines
  has_many :categories, through: :relationships
  belongs_to :user
end

关系模型:

class Relationship < ApplicationRecord
  self.table_name = "rel_anuncio"
  self.primary_key = "id_anuncio"
  belongs_to :ad, class_name: "anuncio", foreign_key: "id_anuncio", optional: true
  belongs_to :category, class_name: "categoria", foreign_key: "id_categoria", optional: true
  belongs_to :subcategory, class_name: "subcategoria", foreign_key: "id_subcategoria", optional: true
end

类别模型:

class Category < ApplicationRecord
  self.table_name = "categorias"
  has_many :subcategories
  has_many :relationships
  has_many :ads, through: :relationships
  belongs_to :user
end

子类别模型:

class Subcategory < ApplicationRecord
  self.table_name = "subcategorias"
  has_many :relationships
  has_many :ads, through: :relationships
  belongs_to :category
end

您可以在上面的模型中看到,我一直在尝试使关系模型分别与类别和子类别模型连接到广告模型,因为这些模型之间具有n:n的关系。以前,当我将英语数据库用作练习时,@categories.first.ads.count可以工作,但是更改为西班牙语数据库后,它突然停止工作。在关系表中,我还为每个模型明确设置了外键。

广告表(广告)架构:

create_table "anuncios", id: :integer, force: :cascade, options: "ENGINE=MyISAM DEFAULT CHARSET=utf8" do |t|
    t.string "empresa", null: false
    t.string "tel", null: false
    t.string "fax_principal", null: false
    t.string "movil_principal", null: false
    t.string "email_principal", null: false
    t.string "web", null: false
    t.string "facebook", null: false
    t.string "horario_v_principal", null: false
    t.string "horario_i_principal", null: false
    t.string "direccion_principal", null: false
    t.string "poblacion_principal", null: false
    t.string "activo", limit: 2, null: false
    t.string "tam_anuncio", null: false
    t.string "twitter", null: false
    t.string "link", limit: 2, null: false
    t.string "general", limit: 2, null: false
    t.string "isla", limit: 10, null: false
    t.string "subtitulo", null: false
    t.string "comentario", null: false
    t.datetime "modificacion", null: false
    t.integer "promo1", default: 0, null: false
    t.integer "promo2", default: 0, null: false
    t.string "instagram", null: false
    t.string "tel2", null: false
    t.string "tel3", null: false
    t.string "tel4", null: false
    t.string "movil2", null: false
    t.string "movil3", null: false
    t.string "movil4", null: false
  end

关系表(rel_anuncios)模式:

create_table "rel_anuncio", primary_key: ["id_anuncio", "id_categoria", "id_subcategoria"], force: :cascade, options: "ENGINE=MyISAM DEFAULT CHARSET=utf8" do |t|
    t.integer "id_anuncio", null: false
    t.integer "id_categoria", null: false
    t.integer "id_subcategoria", null: false
    t.integer "orden", null: false
  end

类别表(类别)架构:

create_table "categorias", id: :integer, force: :cascade, options: "ENGINE=MyISAM DEFAULT CHARSET=utf8" do |t|
    t.string "nombre", null: false
    t.string "color", null: false
    t.string "activo", limit: 2, null: false
    t.string "bdd", limit: 7, null: false
    t.integer "orden", null: false
    t.integer "promoI", limit: 1, default: 0, null: false
    t.integer "promoF", limit: 1, default: 0, null: false
    t.integer "islas", limit: 1, default: 3, null: false
  end

子类别表(子类别)架构:

create_table "subcategorias", id: :integer, force: :cascade, options: "ENGINE=MyISAM DEFAULT CHARSET=utf8" do |t|
    t.integer "id_categoria", null: false
    t.string "nombre", null: false
    t.string "color", null: false
    t.string "activo", limit: 2, default: "si", null: false
    t.integer "orden", null: false
    t.integer "promoI", limit: 1, default: 0, null: false
    t.integer "promoF", limit: 1, default: 0, null: false
    t.integer "islas", limit: 1, default: 3, null: false
  end

更新:

为回应@Jagdeep Singh评论,我将relationship.rb更改为如下形式:

class Relationship < ApplicationRecord
  self.table_name = "rel_anuncio"
  self.primary_key = "id_anuncio"
  belongs_to :ad, foreign_key: "id_anuncio", optional: true
  belongs_to :category, foreign_key: "id_categoria", optional: true
  belongs_to :subcategory, foreign_key: "id_subcategoria", optional: true
end

*我已经删除了班级名称。

此更改后,出现以下错误:

Mysql2::Error: Unknown column 'rel_anuncio.category_id' in 'where clause': SELECT COUNT(*) FROM `anuncios` INNER JOIN `rel_anuncio` ON `anuncios`.`id` = `rel_anuncio`.`id_anuncio` WHERE `rel_anuncio`.`category_id` = 1

在这里,我可以看到ActiveRecord正在其SQL语句category_id中使用它(应为id_categoria(请参见上面的架构关系表)。我不知道如何使ActiveRecord为Foreign_key使用正确的名称。

1 个答案:

答案 0 :(得分:3)

在定义关联时,您的类名称应该是实际的模型名称(而不是其表名称)。当您的关联名称遵循conventions时,例如belongs_to :ad的模型是Ad,依此类推,您可以省略指定class_name

class Relationship < ApplicationRecord
  belongs_to :ad, foreign_key: "id_anuncio", optional: true
  belongs_to :category, foreign_key: "id_categoria", optional: true
  belongs_to :subcategory, foreign_key: "id_subcategoria", optional: true
end

更新

在评论中发布最近的错误之后,关联定义有更多更改:

class Category < ApplicationRecord
  has_many :subcategories, foreign_key: 'id_categoria'
  has_many :relationships, foreign_key: 'id_categoria'
  has_many :ads, through: :relationships
  belongs_to :user
end

class Subcategory < ApplicationRecord
  has_many :relationships, foreign_key: 'id_subcategoria'
  has_many :ads, through: :relationships
  belongs_to :category, foreign_key: 'id_categoria'
end