Question: I am having a lot of problems rewriting this SQL Query in Squeel or straight Active Record using Ruby on Rails 4.

All 3 numbers in the query need to be passed to the query before execution. The SQL Query is below.

```
SELECT "users".* 
FROM "users"  
WHERE "users"."id" 
IN (SELECT "users"."id" FROM "users"  
INNER JOIN marketing_assets ON users.id = marketing_assets.user_id 
WHERE  marketing_assets.marketing_platform_id= 3 
GROUP BY users.id HAVING COUNT(DISTINCT marketing_assets.marketing_platform_id) = 1)

```

You can find out more detail about how it is being used at [Error when trying to chain class method in controller in Ruby on Rails](https://stackoverflow.com/questions/21505042/error-when-trying-to-chain-class-method-in-controller-in-ruby-on-rails)

Answer: 
Use [WeakReference](http://docs.oracle.com/javase/7/docs/api/java/lang/ref/WeakReference.html)s.

In B and C, make your references to A like this:

```
public class B {
    private final WeakReference<A> myA;

    public B (A aToUse) {
        myA = new WeakReference<A>(aToUse);
    }

    private void doSomethingWithMyA() {
        A aToUse = myA.get();  // This returns null when the instance has been GC'd
    }        
}

```

Now, if the instance to A that you're deleting is the only instance of A that isn't a `WeakReference`, then that instance of A becomes eligible for garbage collection. Once it is garbage collected your `WeakReference`s will return `null`;
According to my understanding if you are passing the instance of Class A into B and C and you want that after deleting it this reference is set to null.

Note that first all the classes are loaded by class loader so at compile time the references are checked and if you delete the class A and surely you have declare the reference of A in B and C, so it gives you the compilation error type is missing something like that