Question

I don't pretend to begin understanding i18n and have not had to use it often but here is the concept.

My User class:

class User < ActiveRecord::Base
   has_many :parent_relationships, class_name: 'UserRelationship', foreign_key: 'parent_id'
   has_many :user_relationships, class_name: 'UserRelationship', foreign_key: 'user_id'
   has_many :dealers, through: :parent_relationships
   has_one  :agent, through: :user_relationships
   ...
end

My UserRelationships class:

class UserRelationship < ActiveRecord::Base
   attr_accessible :end_date, :parent_id, :start_date, :user_id

   belongs_to :agent, class_name:'User',conditions:["user_type='Agent' AND id=?",:parent_id]
   belongs_to :dealer, class_name:'User',conditions:["user_type='Dealer' AND id=?",:user_id]
end

I want User.model_name.human to return "User" but when the agent association is referenced I would like it to return "Agent".

I have tried a few things none of which have any success and would be less than useful posting here as they were just stabs in the dark. So my question is 2 fold. Is this possible and if so can someone explain the implementation so I have a better understanding of i18n.

My reason for doing this is I have a ransack form which searches Users but I also would like to have it search Agent as a optgroup and right now it outputs 2 User opt groups This github issue seems to suggest it is possible but the poster did not explain the implementation.

Was it helpful?

Solution

I have figured it out ransack uses it's own namespace for associations in translate.rb line 53

 defaults = key.blank? ? [:"#{context.klass.i18n_scope}.models.#{context.klass.model_name.singular}"] : [:"ransack.associations.#{context.klass.model_name.singular}.#{key}"] 

So the i18n translation would be:

 ransack:
  associations:
    user:
      agent: "Agent"

In case anyone else comes across this issue.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top