我已经构建了一个连接到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
答案 0 :(得分:0)
我找到了答案。我忘记将广告模型中的外键设置为“ id_anuncio”。如果您没有明确告诉Rails外键列的名称不同,那么它将仅通过模型名称+ id:The bean 'myRepository', defined in null, could not be registered. A bean with that name has already been defined in null and overriding is disabled.
进行调用。您可以在控制台内部的错误中看到这一点:ad_id
。
因此,我的广告模型必须具有以下内容:
WHERE anuncios_textos.ad_id