【问题标题】:Using English models with Spanish database in Rails在 Rails 中使用英语模型和西班牙语数据库
【发布时间】:2018-07-18 20:03:30
【问题描述】:

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

广告模型:

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 使用正确的名称。

【问题讨论】:

    标签: mysql ruby-on-rails activerecord


    【解决方案1】:

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

    更新

    最近在 cmets 中发布错误后关联定义的更多变化:

    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
    

    【讨论】:

    • 好的,谢谢@Jagdeep Singh。我已经删除了类名。现在我在页面上收到以下错误: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
    • 谢谢 Jagdeep 成功了。你已经解开了这个谜。
    猜你喜欢
    • 2021-07-24
    • 1970-01-01
    • 1970-01-01
    • 2013-05-13
    • 2011-10-13
    • 1970-01-01
    • 2010-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多