diff --git a/lib/jsonapi/association.rb b/lib/jsonapi/association.rb index bcc98b115..8704d0a2f 100644 --- a/lib/jsonapi/association.rb +++ b/lib/jsonapi/association.rb @@ -7,10 +7,11 @@ def initialize(name, options={}) @options = options @acts_as_set = options.fetch(:acts_as_set, false) == true @foreign_key = options[:foreign_key ] ? options[:foreign_key ].to_sym : nil + @module_path = options[:module_path] || '' end def primary_key - @primary_key ||= Resource.resource_for(@name)._primary_key + @primary_key ||= Resource.resource_for(@module_path + @name)._primary_key end class HasOne < Association diff --git a/lib/jsonapi/resource.rb b/lib/jsonapi/resource.rb index 9daa77d0e..eca620854 100644 --- a/lib/jsonapi/resource.rb +++ b/lib/jsonapi/resource.rb @@ -127,7 +127,7 @@ def _create_has_many_links(association_type, association_key_values) association = self.class._associations[association_type] association_key_values.each do |association_key_value| - related_resource = Resource.resource_for(association.type).find_by_key(association_key_value, context: @context) + related_resource = Resource.resource_for(self.class.module_path + association.type.to_s).find_by_key(association_key_value, context: @context) # ToDo: Add option to skip relations that already exist instead of returning an error? relation = @model.send(association.type).where(association.primary_key => association_key_value).first @@ -203,11 +203,6 @@ def inherited(base) base.attribute :id, format: :id check_reserved_resource_name(base._type, base.name) - - # If eager loading is on this is how all the resource types are setup - # If eager loading is off some resource types will be initialized in - # _resource_name_from_type - @@resource_types[base._type] ||= base.name.demodulize end def resource_for(type) @@ -518,6 +513,7 @@ def check_reserved_association_name(name) def _associate(klass, *attrs) options = attrs.extract_options! + options[:module_path] = module_path attrs.each do |attr| check_reserved_association_name(attr) diff --git a/test/test_helper.rb b/test/test_helper.rb index b2ca32e53..c30f454b8 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -27,7 +27,6 @@ puts "Testing With RAILS VERSION #{Rails.version}" class TestApp < Rails::Application - config.eager_load = false config.root = File.dirname(__FILE__) config.session_store :cookie_store, key: 'session' config.secret_key_base = 'secret'