【问题标题】:How are I18n and database ActiveRecord querying related?I18n 和数据库 ActiveRecord 查询有什么关系?
【发布时间】:2016-05-03 01:26:36
【问题描述】:

有谁知道,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

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

【问题讨论】:

  • Rails I18n 模块实际上与数据库没有任何关系——相反,当 ActiveRecord::Collection 被初始化时,您有一些东西会导致查找。但是,如果没有堆栈跟踪,就不可能说出原因。
  • @max 用真实的代码更新了这个问题,谢谢看看
  • @AndreyDeineko - 你在控制台中得到这些错误吗?如果是这样,它可能与检查方法有关。
  • 我看到的唯一明显的事情是has_many :decorative_centers_sales_representative 的复数形式不正确。使用decorative_centers_sales_representatives
  • @BroiSatse 是的,这些方法来自控制台

标签: mysql ruby-on-rails ruby-on-rails-4 has-many-through rails-i18n


【解决方案1】:

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

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 

让它按预期工作。

【讨论】:

  • 很好,但是您通常使用字符串作为类名而不是类本身。 class_name: "DecorativeCenter::SalesRepresentative"stackoverflow.com/questions/20390991/…
  • 这是如何导致 I18n 问题的?
  • @BroiSatse,事实上这个解决方案只工作了一天,不知何故后来它不起作用,所以我不得不努力(呃):) 我会用真正的解决方案更新答案。
猜你喜欢
  • 1970-01-01
  • 2016-06-10
  • 2016-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多