# Include NULL values using :group option?

**URL:** <https://www.ruby-forum.com/t/include-null-values-using-group-option/182357>\
**Category:** Rails\
**Created:** [January 27, 2010, 12:41am UTC](https://www.ruby-forum.com/t/include-null-values-using-group-option/182357 "2010-01-27T00:41:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![polg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f1d935/32.png) [@polg](https://www.ruby-forum.com/u/polg)\
**Post date:** [January 27, 2010, 12:41am UTC](https://www.ruby-forum.com/t/include-null-values-using-group-option/182357/1 "2010-01-27T00:41:58Z")

</div>

I’m trying to use the :group option in an ActiveRecord association. I  
wrote the code as follows:

class User \< ActiveRecord::Base  
has\_many :messages, :group =\> “thread”, :order =\> “created\_at desc”  
end

It works fine as it returns some records. The problem is that the mysql  
clause “GROUP BY” ignores NULL values. That makes that records with  
thread == NULL not to show.

Â¿Is there a rails option to include NULL values when using :group?

Thanks,  
Pablo

---

<div class="post-metadata">

**Author:** ![polg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f1d935/32.png) [@polg](https://www.ruby-forum.com/u/polg)\
**Post date:** [January 27, 2010, 4:33pm UTC](https://www.ruby-forum.com/t/include-null-values-using-group-option/182357/2 "2010-01-27T16:33:34Z")

</div>

On Jan 26, 6:41 pm, Pablo G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ¿Is there a rails option to include NULL values when using :group?

Given that the underlying DB doesn’t appear to support the operation,  
I’d say not. Even if it did, the result would be that you’d only get  
the most recent post with thread == NULL, which doesn’t seem like what  
you’d want.

–Matt J.

---

<div class="post-metadata">

**Author:** ![polg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f1d935/32.png) [@polg](https://www.ruby-forum.com/u/polg)\
**Post date:** [January 27, 2010, 5:02pm UTC](https://www.ruby-forum.com/t/include-null-values-using-group-option/182357/3 "2010-01-27T17:02:46Z")

</div>

I think you’re right. I’ve been looking for that solution and I didn’t  
find anything really solving it. So I guess I must figure out another  
approuch, perhaps giving a default value for “thread”.

Thank you anyway.
