mysql - 在 Rails 中使用带有西类牙语数据库的英语模型

标签 mysql ruby-on-rails activerecord

我刚刚将我的 Rails 应用程序链接到一个 mysql 数据库,该数据库具有西类牙语的表名和列。现在,我通过在 model.rb 中设置 self.table_name = "table_name" 解决了西类牙文表名问题。现在,当我想通过连接表调用数据时,会出现下一个问题。在这种情况下,我尝试调用属于第一个 category 的所有 ads。正如您在下面的屏幕截图中看到的那样,当我尝试此操作时,出现此错误。它现在看到 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 可以正常工作,但更改为西类牙文数据库时它突然停止工作。在关系表中,我还为每个模型明确设置了外键。

广告表 (anuncios) 架构:

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

类别表(categorias)模式:

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

子类别表(subcategorias)模式:

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 名称。

最佳答案

在定义关联时,您的类名应该是实际的模型名(而不是它们的表名)。随着您的协会名称遵循 rails 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

关于mysql - 在 Rails 中使用带有西类牙语数据库的英语模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51410164/

相关文章:

mysql - 将多行合并为一行

javascript - 如何将mysql值存储到变量中?

mysql - 无法在mysql中创建存储过程

ruby-on-rails - 无法将 CKeditor 集成到 rails_admin gem(使用 gem rich)

mysql - 连接两个表时检索所有数据并按项目计数

mysql 5.7.11 恢复为 myisam

ruby-on-rails - 除了 id 之外的 Rails 外键

ruby-on-rails - 如何在 Ubuntu 16.04 上安装 mysql2 [错误 : Error installing mysql2: ERROR: Failed to build gem native extension.]

ruby-on-rails - RAILS : Does ActiveRecord#persisted? 实际查询数据库?

ruby-on-rails - 新手:获取对象的属性值为#<ActiveRecord::Relation> 提供错误未定义的方法