gpt4 book ai didi

mysql - I18n 和数据库 ActiveRecord 查询有什么关系?

转载 作者:行者123 更新时间:2023-11-30 22:26:38 24 4
gpt4 key购买 nike

有谁知道,I18n 和数据库有什么关系?

class DecorativeCentersSalesRepresentative < ActiveRecord::Base
belongs_to :decorative_center, class_name: ::DecorativeCenter
belongs_to :user, class_name: ::SalesRepresentative
end

class DecorativeCenter < ActiveRecord::Base
has_many :decorative_centers_sales_representative
has_many :sales_representatives,
through: :decorative_centers_sales_representative
end

class SalesRepresentative < User
has_many :decorative_centers_sales_representative,
foreign_key: :user_id
has_many :decorative_centers,
through: :decorative_centers_sales_representative,
foreign_key: :user_id
end

一切都很好,我能做到

SalesRepresentative.last.decorative_centers
SalesRepresentative Load (0.7ms) SELECT `users`.* FROM `users` WHERE `users`.`type` IN ('SalesRepresentative') ORDER BY `users`.`id` DESC LIMIT 1
DecorativeCenter Load (0.3ms) SELECT `decorative_centers`.* FROM `decorative_centers` INNER JOIN `decorative_centers_sales_representative` ON `decorative_centers`.`id` = `decorative_centers_sales_representative`.`decorative_center_id` WHERE `decorative_centers_sales_representative`.`user_id` = 4
#=> [#<DecorativeCenter:0x000000088e5578]

但是当我这样做的时候

DecorativeCenter.last.sales_representatives
DecorativeCenter Load (0.2ms) SELECT `decorative_centers`.* FROM `decorative_centers` ORDER BY `decorative_centers`.`id` DESC LIMIT 1
#=> I18n::InvalidLocale: :en is not a valid locale
#=> from /home/andreydeineko/.rvm/gems/ruby-2.3.0@profill-base/gems/i18n-0.7.0/lib/i18n.rb:284:in `enforce_available_locales!'

为什么??

我知道这是一个无效的语言环境,有效的是 :pl:

I18n.available_locales
#=> [:pl]
I18n.default_locale
#=> :pl

但是这些东西到底有什么关系,为什么我可以用一种方式查询,而不能用其他方式查询?

最佳答案

经过一段时间的调试,我发现了真正的问题。

Starting from the end :

  class HasManyThroughSourceAssociationNotFoundError < ActiveRecordError #:nodoc:
def initialize(reflection = nil)
if reflection
through_reflection = reflection.through_reflection
source_reflection_names = reflection.source_reflection_names
source_associations = reflection.through_reflection.klass._reflections.keys
super("Could not find the source association(s) #{source_reflection_names.collect(&:inspect).to_sentence(:two_words_connector => ' or ', :last_word_connector => ', or ', :locale => :en)} in model #{through_reflection.klass}. Try 'has_many #{reflection.name.inspect}, :through => #{through_reflection.name.inspect}, :source => <name>'. Is it one of #{source_associations.to_sentence(:two_words_connector => ' or ', :last_word_connector => ', or ', :locale => :en)}?")
else
super("Could not find the source association(s).")
end
end
end

在这个错误中,语言环境是硬编码的,它是 :en,这就是为什么我什至无法收到错误消息的原因。

1) 在我的应用程序中 :en 不在可用的语言环境中,所以为了得到 Rails 试图吐出的错误消息,我暂时将应用程序的语言环境设置为 :en

2) 现在我可以得到错误:

ActiveRecord::HasManyThroughSourceAssociationNotFoundError: Could not find the source association(s) "sales_representative" or :sales_representatives in model DecorativeCentersSalesRepresentatives. Try 'has_many :sales_representatives, :through => :decorative_centers_sales_representatives, :source => <name>'. Is it one of versions, decorative_center, or user?

这只是说明,我在连接表中写 belongs_to 是错误的。

AR 期望定义关联的名称,而不是数据库中的表。

变化多端

# (STI) table users, but AR model is called SalesRepresentative 
belongs_to :user, class_name: ::SalesRepresentative

# changed to real AR table name passing the foreign_key
belongs_to :sales_representative, class_name: ::SalesRepresentative, foreign_key: :user_id

让它按预期工作。

关于mysql - I18n 和数据库 ActiveRecord 查询有什么关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35014586/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com