# Master table without related child record

**URL:** <https://www.ruby-forum.com/t/master-table-without-related-child-record/187240>\
**Category:** Rails\
**Created:** [April 20, 2010, 2:21pm UTC](https://www.ruby-forum.com/t/master-table-without-related-child-record/187240 "2010-04-20T14:21:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![f2andy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/7feea3/32.png) [@f2andy](https://www.ruby-forum.com/u/f2andy)\
**Post date:** [April 20, 2010, 2:21pm UTC](https://www.ruby-forum.com/t/master-table-without-related-child-record/187240/1 "2010-04-20T14:21:27Z")

</div>

Having a bit of a braindead day, can anyone help with this. I have  
three  
models: Player, Game, Prediction (Player has\_many :games, :through =\>  
:prediction)

I want a named\_scope that finds games that the current player hasn’t  
already  
predicted.

I started with something like this:

named\_scope :without\_prediction, lambda { |player\_id|  
{  
:select =\> “games._, predictions._”,  
:joins =\> “LEFT JOIN predictions ON predictions.game\_id =  
games.id”,  
:conditions =\> “predictions.player\_id IS NULL”  
}  
}

The problem is that this won’t work if ANY player has predicted that  
game’s  
result. So to be clear if there are games 1, 2 and 3 and players 10 and  
11,  
player 10 has predicted results for all 3 games and player 10 has only  
predicted 1 and 2, I want a named\_scope that returns game 3.

It seems like this is something I’ve done over and over again, but I  
just  
can’t get it clear in my head today.

Any ideas?

Cheers,

Andy

–  
Andy J.  
[http://andyjeffries.co.uk/](http://andyjeffries.co.uk/)#rubyonrails #mysql #jquery  
Registered address: 64 Sish Lane, Stevenage, Herts, SG1 3LS  
Company number: 5452840

---

<div class="post-metadata">

**Author:** ![f2andy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/7feea3/32.png) [@f2andy](https://www.ruby-forum.com/u/f2andy)\
**Post date:** [April 20, 2010, 3:06pm UTC](https://www.ruby-forum.com/t/master-table-without-related-child-record/187240/2 "2010-04-20T15:06:33Z")

</div>

On Tue, Apr 20, 2010 at 2:19 PM, Andy J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Having a bit of a braindead day, can anyone help with this. I have three  
> models: Player, Game, Prediction (Player has\_many :games, :through =\>  
> :prediction)  
> I want a named\_scope that finds games that the current player hasn’t already  
> predicted.

How about tackling it from the Player model like this

def unpredicted\_games  
Game.all - self.games  
end

---

<div class="post-metadata">

**Author:** ![f2andy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/7feea3/32.png) [@f2andy](https://www.ruby-forum.com/u/f2andy)\
**Post date:** [April 20, 2010, 3:17pm UTC](https://www.ruby-forum.com/t/master-table-without-related-child-record/187240/3 "2010-04-20T15:17:40Z")

</div>

> def unpredicted\_games  
> Game.all - self.games  
> end

That’ll do me…

This is only a site for about 30-40 friends, so I’m not too bothered  
about  
efficiency.

Cheers,

Andy

---

<div class="post-metadata">

**Author:** ![f2andy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/7feea3/32.png) [@f2andy](https://www.ruby-forum.com/u/f2andy)\
**Post date:** [April 20, 2010, 4:54pm UTC](https://www.ruby-forum.com/t/master-table-without-related-child-record/187240/4 "2010-04-20T16:54:18Z")

</div>

On Apr 20, 1:19 pm, Andy J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> {
> 
> ```

change the join so that the condition is predictions.game\_id =  
games.id and predictions.player\_id = …  
and then change it so that the condition is predictions.id is NULL.  
Also be careful when doing something like games._, predictions._ -  
identically named columns will shadow each other (eg id) which can  
lead to strange bugs

Fred
