```ruby
module WulinMaster
  module GridRelation
    extend ActiveSupport::Concern
    
    module ClassMethods
      # Set master grid, invoked from grid.apply_custom_config method
      def master_grid(master_grid_klass, options={})
        if options[:screen]
          detail_model = self.model
          master_grid = master_grid_klass.constantize.new({screen: options[:screen], format:'json'})   # format as json to skip the toolbar and styling initialize
          through = options[:through] || detail_model.reflections[master_grid.model.to_s.underscore.intern].foreign_key

          # disable the multiSelect for master grid
          master_grid_klass.constantize.options_pool << {:multiSelect => false, :only => [options[:screen].intern]}

          # call affiliation behavior for detail grid
          behavior :affiliation, master_grid_name: master_grid.name, only: [options[:screen].intern], through: through
        end
      end
    end
    
    # ----------------------------- Instance Methods ------------------------------------
    
  end
end```

Refactor 30m - master-detail grid, add the case of has_and_belongs_to_many
user
```ruby
module WulinMaster
  module GridRelation
    extend ActiveSupport::Concern
    
    module ClassMethods
      # Set master grid, invoked from grid.apply_custom_config method
      def master_grid(master_grid_klass, options={})
        if options[:screen]
          detail_model = self.model
          master_grid = master_grid_klass.constantize.new({screen: options[:screen], format:'json'})   # format as json to skip the toolbar and styling initialize
          through = options[:through] || 
                    detail_model.reflections[master_grid.model.to_s.underscore.intern].try(:foreign_key) ||     # detail_model belongs_to master_model
                    detail_model.reflections[master_grid.model.to_s.underscore.pluralize.intern].try(:foreign_key)    # detail_model has_many master_model

          # disable the multiSelect for master grid
          master_grid_klass.constantize.options_pool << {:multiSelect => false, :only => [options[:screen].intern]}

          # call affiliation behavior for detail grid
          behavior :affiliation, master_grid_name: master_grid.name, only: [options[:screen].intern], through: through
        end
      end
    end
    
    # ----------------------------- Instance Methods ------------------------------------
    
  end
end```
assistant