【发布时间】: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