I18n和数据库ActiveRecord查询如何相关?

时间:2016-01-26 13:09:23

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

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

WHY ??

我知道这是一个无效的区域设置,有效区域为:pl

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

但是这些事情甚至是如何相关的,为什么我可以查询一种方式,而不能查询其他方式?

1 个答案:

答案 0 :(得分:0)

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

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 

使其按预期工作。