代码之家  ›  专栏  ›  技术社区  ›  OKMantis

在rails中使用英语模型和西班牙语数据库

  •  0
  • OKMantis  · 技术社区  · 6 年前

    我刚刚将我的Rails应用程序链接到一个mysql数据库,这个数据库有西班牙语的表名和列现在我通过设置 self.table_name = "table_name" 在里面 model.rb 是的。现在,当我想通过连接表来调用数据时,下一个问题就会出现。在这种情况下,我想打电话给 ads 属于第一个 category 是的。当我像你在下面的截图中看到的那样尝试时,我得到了这个错误。它看到了 ad 现在就坐 anuncio 这是西班牙语的名字。我有点困惑,因为我觉得 self.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 工作了,但换成了西班牙语数据库,它突然停止了工作在relationship表中,我还显式地为每个模型设置外键。

    ADS表(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
    

    类别表(类别)架构:

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

    1 回复  |  直到 6 年前
        1
  •  3
  •   Jagdeep Singh    6 年前

    你的类名应该是 型号名称 (而不是它们的表名)定义关联时。当你的协会名称遵循轨道 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