I’ve pasted the code where I have a problem here:
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?
Scott Porad wrote:
I’ve pasted the code where I have a problem here:
Progress vs. Perfection: Issue with has_many and Array.empty?
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
Josh–
I didn’t file a ticket. What is the ticket you are referring to?
Scott
On Nov 8, 9:37 pm, Josh S. [email protected]
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
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
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!
–
Josh S.
http://blog.hasmanythrough.com