Permalink
Browse files

Honor primary_key class properties

  • Loading branch information...
denyago committed Nov 26, 2012
1 parent c97f942 commit cc371aa2341fae9a7a99df7b01985dcbe2f15472
@@ -103,9 +103,10 @@ def fetch_and_join_for_has_any_remote(settings)
keys = @records.uniq.map(&:id)
remote_objects = fetch_remote_objects(settings.ar_class, keys, settings.ar_accessor)
+ join_key = klass.primary_key
@records.each do |record|
- record.send("#{settings.ar_accessor}=", remote_objects.select { |s| s.send(settings.foreign_key) == record.id })
+ record.send("#{settings.ar_accessor}=", remote_objects.select { |s| s.send(settings.foreign_key) == record.send(join_key) })
end
end
@@ -115,9 +116,10 @@ def fetch_and_join_for_belongs_to_remote(settings)
return if keys.empty?
remote_objects = fetch_remote_objects(settings.ar_class, keys, settings.ar_accessor)
+ join_key = settings.ar_class.primary_key
@records.each do |record|
- record.send("#{settings.ar_accessor}=", remote_objects.select { |s| record.send(settings.foreign_key) == s.id })
+ record.send("#{settings.ar_accessor}=", remote_objects.select { |s| record.send(settings.foreign_key) == s.send(join_key) })
end
end
@@ -48,7 +48,7 @@ def belongs_to_remote(remote_rel, options ={})
class_name: remote_rel.to_s.classify,
foreign_key: remote_rel.to_s.foreign_key,
association_type: :belongs_to_remote,
- primary_key: 'id'
+ primary_key: primary_key
}.merge(options.symbolize_keys)
add_activeresource_relation(remote_rel.to_sym, rel_options)
@@ -54,8 +54,10 @@ def #{remote_rel} # def custome
if remote_resources_loaded? # if remote_resources_loaded?
@#{remote_rel} ? @#{remote_rel} : [] # @customers ? @customers : []
else # else
+ join_key = self.class.primary_key # join_key = self.class.primary_key
@#{remote_rel} ||= #{rel_options[:class_name]}. # @customers ||= Person.
- find(:all, params: self.class.build_params_hash(self.id)) # find(:all, params: self.class.build_params_hash(self.id))
+ find(:all, params: self.class. # find(:all, params: self.class.
+ build_params_hash(self.send(join_key))) # build_params_hash(self.send(join_key)))
end # end
end # end
@@ -54,7 +54,8 @@ def #{remote_rel}
if remote_resources_loaded?
@#{remote_rel} ? @#{remote_rel}.first : nil
else
- @#{remote_rel} ||= #{rel_options[:class_name]}.find(:first, params: self.class.build_params_hash(self.id))
+ join_key = self.class.primary_key
+ @#{remote_rel} ||= #{rel_options[:class_name]}.find(:first, params: self.class.build_params_hash(self.send(join_key)))
end
end

0 comments on commit cc371aa

Please sign in to comment.