【发布时间】:2018-12-05 17:33:35
【问题描述】:
我已经构建了一个连接到 mysql 数据库的 rails 应用程序,该数据库作为其表名和西班牙语列。我已经制作了代表具有英文名称的表的模型,并将每个模型放入 self.table_name = "table_name_in_spanish" 以将表转换为模型名称,这样 Rails 约定就不会被破坏。考虑到这一点,我有一个模型Ad 和一个模型AdCopy,其中包含Ad 模型的多个描述。当我想获取一个广告的这些描述列表时,我在 rails c 中执行以下操作:
我首先将广告分配给变量:
pry(main)> ad = Ad.last
Ad Load (0.4ms) SELECT `anuncios`.* FROM `anuncios` ORDER BY `anuncios`.`id` DESC LIMIT 1
=> #<Ad:0x00007ff8dc45e2f0
id: 1241,
empresa: "Example Ad",
tel: "555 555 555",
[...]
然后尝试访问广告副本(描述属于广告):
pry(main)> ad.ad_copies
AdCopy Load (1.5ms) SELECT `anuncios_textos`.* FROM `anuncios_textos` WHERE `anuncios_textos`.`ad_id` = 1241
AdCopy Load (1.3ms) SELECT `anuncios_textos`.* FROM `anuncios_textos` WHERE `anuncios_textos`.`ad_id` = 1241 LIMIT 11
=> #<AdCopy::ActiveRecord_Associations_CollectionProxy:0x3ffc6d8fc498>
这里的内容已经让我感到困惑,因为我不知道 ActiveRecord_Associations_CollectionProxy 是什么。
所以最后一步是尝试显示第一个描述:
pry(main)> ad.ad_copies.first
AdCopy Load (1.3ms) SELECT `anuncios_textos`.* FROM `anuncios_textos` WHERE `anuncios_textos`.`ad_id` = 1241 ORDER BY `anuncios_textos`.`id_anuncio` ASC LIMIT 1
ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column 'anuncios_textos.ad_id' in 'where clause': SELECT `anuncios_textos`.* FROM `anuncios_textos` WHERE `anuncios_textos`.`ad_id` = 1241 ORDER BY `anuncios_textos`.`id_anuncio` ASC LIMIT 1
from /Users/okmantis/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/mysql2-0.4.10/lib/mysql2/client.rb:120:in `_query'
这就是问题所在。我如何能够访问属于我的广告的描述?
这是我的模型:
class Ad < ApplicationRecord
self.table_name = "anuncios"
has_many :ad_copies
has_many :ad_addresses
has_many :relationships, foreign_key: 'id_anuncio'
has_many :magazines
has_many :categories, through: :relationships
belongs_to :user
end
class AdCopy < ApplicationRecord
self.table_name = "anuncios_textos"
self.primary_key = "id_anuncio"
belongs_to :ad, foreign_key: "id_anuncio", optional: true
belongs_to :language, foreign_key: "id_idioma", optional: true
end
模型广告的模式表(西班牙语:anuncios):
create_table "anuncios", id: :integer, options: "ENGINE=MyISAM DEFAULT CHARSET=utf8", force: :cascade 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
模型 AdCopy 的架构表(西班牙语:anuncios_textos):
create_table "anuncios_textos", primary_key: ["id_anuncio", "id_idioma"], options: "ENGINE=MyISAM DEFAULT CHARSET=utf8", force: :cascade do |t|
t.integer "id_anuncio", null: false
t.integer "id_idioma", null: false
t.text "descripcion", null: false
end
【问题讨论】:
标签: mysql ruby-on-rails activerecord