greenif
greenif

Reputation: 1093

Rails - exception I18n::InvalidLocale in a strange place

Sometimes rails is beautiful, but sometimes it's terrible. Especially when you got strange exception

I have 3 simple models

/app/models/specification_type.rb

class SpecificationType  < ActiveRecord::Base#< AbstractModel

  has_many :scpecification_type_to_category_relations, class_name: "Relations::SpecificationTypeToCategory"
  has_many :categories, through: :scpecification_type_to_category_relations  

end

/app/models/category.rb

class Category < ActiveRecord::Base#AbstractModel

  has_many :groups
  has_many :products

  has_many :scpecification_type_to_category_relations, class_name: "Relations::SpecificationTypeToCategory"
  has_many :specification_types, through: :scpecification_type_to_category_relations  

end

/app/models/relations/specification_type_to_category.rb

class Relations::SpecificationTypeToCategory < ActiveRecord::Base

  self.table_name = "specification_type_to_category_relations"

  belongs_to :scpecification_type
  belongs_to :category

end

Migration 20141231115801_create_specification_types.rb

Each time anywhere(view or console), when I try to call @category.specification_types I get exception:

I18n::InvalidLocale: :en is not a valid locale

rails c

Category.first.specification_types
  Category Load (0.3ms)  SELECT  `categories`.* FROM `categories`  ORDER BY `categories`.`id` ASC LIMIT 1
I18n::InvalidLocale: :en is not a valid locale
        from /home/anton/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/i18n-0.7.0/lib/i18n.rb:284:in `enforce_available_locales!'
        from /home/anton/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/i18n-0.7.0/lib/i18n.rb:151:in `translate'
        from /home/anton/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/activesupport-4.2.0/lib/active_support/core_ext/array/conversions.rb:68:in `to_sentence'
        from /home/anton/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-4.2.0/lib/active_record/associations.rb:60:in `initialize'
        from /home/anton/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-4.2.0/lib/active_record/reflection.rb:840:in `new'
        from /home/anton/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-4.2.0/lib/active_record/reflection.rb:840:in `check_validity!'
        from /home/anton/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-4.2.0/lib/active_record/associations/association.rb:25:in `initialize'
....

I don't understand where does the I18n

I use I18n in the project, but not here. There are 2 locales, but I do not use :en locale at all.

Help me please.

Upvotes: 4

Views: 970

Answers (1)

greenif
greenif

Reputation: 1093

The mistake was very primitive. Extra letter "c" in a :specification type. But to see human readable exceptions I set in application.rb

config.i18n.enforce_available_locales = false

Upvotes: 5

Related Questions