Actually this should work:

```ruby
if records == :all || !reflection.klass.primary_key
  scope = self.scope
else
  scope = self.scope.where(reflection.klass.primary_key => records)
end
```

Since the `self.scope` already has the information about the `foreign_key`