# Possible bug with has\_many and Array#empty?

**URL:** <https://www.ruby-forum.com/t/possible-bug-with-has-many-and-array-empty/119669>\
**Category:** Rails\
**Created:** [November 9, 2007, 1:51am UTC](https://www.ruby-forum.com/t/possible-bug-with-has-many-and-array-empty/119669 "2007-11-09T01:51:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dark-omen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c68b51/32.png) [@dark-omen](https://www.ruby-forum.com/u/dark-omen)\
**Post date:** [November 9, 2007, 1:51am UTC](https://www.ruby-forum.com/t/possible-bug-with-has-many-and-array-empty/119669/1 "2007-11-09T01:51:16Z")

</div>

I’ve pasted the code where I have a problem here:

> **[Issue with has\_many and Array.empty?](http://progress-vs-perfection.blogspot.com/2007/11/issue-with-arrayempty.html)**
>
> I've posted a narrative of my issue on the Google Rails Group : /app/controllers/document\_groups\_controller.rb def show @document\_grou...

The bottom line is this: I have an object called document\_group that  
has many document\_templates. In my controller, I create class  
variable like so:

@document\_group\_templates = @document\_group.document\_templates

Then, in my view I have to decide to do some rendering, so I do this:

if !@document\_group\_templates.empty?  
…do stuff  
end

The issue is that .empty? always evaluates to true. For whatever  
reason, the collection of document\_templates on the document\_group  
does not get initialized.

I can look in my log file and see that the SQL to populate the array  
is not triggered. That said,

a) if I do something such as @document\_group\_templates.inspect that  
triggers the SQL to run that populates the array

b) if I do

if @document\_group\_templates.count \> 0  
…do stuff  
end

the #count method also triggers the SQL to run.

All that said, shouldn’t this statement:

@document\_group\_templates = @document\_group.document\_templates

have triggered the SQL to load the array in the first place? Is that  
a bug of some sort?

---

<div class="post-metadata">

**Author:** ![dark-omen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c68b51/32.png) [@dark-omen](https://www.ruby-forum.com/u/dark-omen)\
**Post date:** [November 9, 2007, 6:37am UTC](https://www.ruby-forum.com/t/possible-bug-with-has-many-and-array-empty/119669/2 "2007-11-09T06:37:54Z")

</div>

Scott Porad wrote:

> I’ve pasted the code where I have a problem here:  
> [Progress vs. Perfection: Issue with has\_many and Array.empty?](http://progress-vs-perfection.blogspot.com/2007/11/issue-with-arrayempty.html)
> 
> The bottom line is this: I have an object called document\_group that  
> has many document\_templates. In my controller, I create class  
> variable like so:
> 
> @document\_group\_templates = @document\_group.document\_templates
> 
> Then, in my view I have to decide to do some rendering, so I do this:
> 
> if !@document\_group\_templates.empty?  
> …do stuff  
> end
> 
> The issue is that .empty? always evaluates to true. For whatever  
> reason, the collection of document\_templates on the document\_group  
> does not get initialized.
> 
> I can look in my log file and see that the SQL to populate the array  
> is not triggered. That said,
> 
> a) if I do something such as @document\_group\_templates.inspect that  
> triggers the SQL to run that populates the array
> 
> b) if I do
> 
> if @document\_group\_templates.count \> 0  
> …do stuff  
> end
> 
> the #count method also triggers the SQL to run.
> 
> All that said, shouldn’t this statement:
> 
> @document\_group\_templates = @document\_group.document\_templates
> 
> have triggered the SQL to load the array in the first place? Is that  
> a bug of some sort?

A ticket on track, a blog post, and an email to the list. You’ve  
certainly got it covered. I understand this bug is probably a big  
problem for you and you want to make sure people know about it. But  
there are plenty of us who read the tickets on trac and are on top of  
it. Please see my comment on the ticket for my response.

Please don’t consider this a stern talking to or anything like that. We  
appreciate you trying to make Rails better. I’m just trying to keep you  
from unnecessarily duplicating your communications. In the future if you  
open a ticket and it doesn’t get a response, then you can try to  
escalate. And in that case, it’s better to use rubyonrails-core than  
this list.

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![dark-omen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c68b51/32.png) [@dark-omen](https://www.ruby-forum.com/u/dark-omen)\
**Post date:** [November 9, 2007, 8:43pm UTC](https://www.ruby-forum.com/t/possible-bug-with-has-many-and-array-empty/119669/3 "2007-11-09T20:43:13Z")

</div>

Josh–

I didn’t file a ticket. What is the ticket you are referring to?

Scott

On Nov 8, 9:37 pm, Josh S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![dark-omen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c68b51/32.png) [@dark-omen](https://www.ruby-forum.com/u/dark-omen)\
**Post date:** [November 10, 2007, 2:11am UTC](https://www.ruby-forum.com/t/possible-bug-with-has-many-and-array-empty/119669/4 "2007-11-10T02:11:30Z")

</div>

I’m not sure, but I don’t think that is the same issue.

Ticket 10115 and 8049 address cases where:  
– an object is being created using #new,  
– another object is being appended to an array of which the new  
object has many.  
– #empty? is being evaluated before the object has been saved to  
the DB

In my case, the code is:  
– creating an object A using #find,  
– the object B in the array on object A already existed as data in  
the database  
– then I’m evaluating #empty?

* * *

Scott Porad  
[http://progress-vs-perfection.blogspot.com](http://progress-vs-perfection.blogspot.com)

---

<div class="post-metadata">

**Author:** ![dark-omen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c68b51/32.png) [@dark-omen](https://www.ruby-forum.com/u/dark-omen)\
**Post date:** [November 10, 2007, 12:38pm UTC](https://www.ruby-forum.com/t/possible-bug-with-has-many-and-array-empty/119669/5 "2007-11-10T12:38:32Z")

</div>

On 10 Nov 2007, at 01:10, Scott wrote:

> In my case, the code is:  
> – creating an object A using #find,  
> – the object B in the array on object A already existed as data in  
> the database  
> – then I’m evaluating #empty?

Stab in the dark: if you remove your use of counter cache, does the  
problem go away?

Fred

---

<div class="post-metadata">

**Author:** ![dark-omen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c68b51/32.png) [@dark-omen](https://www.ruby-forum.com/u/dark-omen)\
**Post date:** [November 9, 2007, 10:54pm UTC](https://www.ruby-forum.com/t/possible-bug-with-has-many-and-array-empty/119669/6 "2007-11-09T22:54:17Z")

</div>

Scott Porad wrote:

> I didn’t file a ticket. What is the ticket you are referring to?

This is the ticket: [http://dev.rubyonrails.org/ticket/10115](http://dev.rubyonrails.org/ticket/10115)

Sorry I got you confused with the ticket author!

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)
