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`;
You need some managing mechanism, if you want to control links to your instance.
Without 3rd party solutions, easiest way to do it, is to provide intermidiate container object, which will control access to your instance.

Here is a basic example:

```
public static void main (String[] args) {
    DogContainer dc = new DogContainer(new Dog("Scooby-Doo"));
    Person shaggy = new Person("Shaggy");
    shaggy.setDogContainer(dc);
    Person dafna = new Person("Dafna");
    dafna.setDogContainer(dc);

    System.out.println(shaggy.getName() + " -> " + shaggy.getDogName());
    System.out.println(dafna.getName() + " -> " + dafna.getDogName());

    // Something happened with Scooby here. Say, a bus factor.
    dc.setDog(null);

    System.out.println(shaggy.getName() + " -> " + shaggy.getDogName());
    System.out.println(dafna.getName() + " -> " + dafna.getDogName());
}

static class Dog {
    private String name;
    public Dog(String name) { this.name = name; }
    public String getName() { return name; }
}

static class DogContainer {
    private Dog dog;
    public DogContainer(Dog dog) { this.dog = dog; }
    public Dog getDog() { return dog; }
    public void setDog(Dog dog) { this.dog = dog; }
}

static class Person {
    private String name;
    private DogContainer dogContainer;
    public Person(String name) { this.name = name; }
    public String getName() { return name; }
    public void setDogContainer(DogContainer dc) { this.dogContainer = dc; }
    public String getDogName() {
        Dog dog = dogContainer.getDog();
        return dog == null ? "Sorry, this person is not a dog owner" : dog.getName();
    }
}

```

Here we have three entities: a dog, a person and a dog container.
The person is the dog owner. But he has no direct link to the dog. It can interact with the dog only through the container.
So if you change link into container it will affect all persons.

The output is:

```
Shaggy -> Scooby-Doo
Dafna -> Scooby-Doo
Shaggy -> Sorry, this person is not a dog owner
Dafna -> Sorry, this person is not a dog owner

```

P.S. Also using of nulls is a bad practice, so it's better to use `Special Case` pattern to provide `Null Object`.