Possible bug with has_many and Array#empty?

3 views
Skip to first unread message

Scott

unread,
Nov 8, 2007, 7:50:45 PM11/8/07
to Ruby on Rails: Talk

I've pasted the code where I have a problem here:
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?

Josh Susser

unread,
Nov 9, 2007, 12:37:54 AM11/9/07
to rubyonra...@googlegroups.com

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 Susser
http://blog.hasmanythrough.com
--
Posted via http://www.ruby-forum.com/.

Scott

unread,
Nov 9, 2007, 2:40:07 PM11/9/07
to Ruby on Rails: Talk

Josh--

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

Scott

On Nov 8, 9:37 pm, Josh Susser <rails-mailing-l...@andreas-s.net>
wrote:


> Scott Porad wrote:
> > I've pasted the code where I have a problem here:

> >http://progress-vs-perfection.blogspot.com/2007/11/issue-with-arrayem...

Josh Susser

unread,
Nov 9, 2007, 4:54:17 PM11/9/07
to rubyonra...@googlegroups.com
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

Sorry I got you confused with the ticket author!

Scott

unread,
Nov 9, 2007, 8:10:47 PM11/9/07
to Ruby on Rails: Talk

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

Frederick Cheung

unread,
Nov 10, 2007, 6:37:40 AM11/10/07
to rubyonra...@googlegroups.com, Scott

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

Reply all
Reply to author
Forward
0 new messages