I don't see why the count tag would be loading your whole association.
Here's the full line:
c = this.try.to_int || this.try.total_entries || (this.try.loaded? &&
this.try.length) || this.try.count || this.try.length
piece by piece:
this.try.to_int: would never load an association
this.try.total_entries: just an attr_reader, will never load
(this.try.loaded? && this.try.length): won't load if it's not already
loaded
this.try.count: would fire off a COUNT but won't load
anything
this.try.length: finally, yes, this would load the assoc
So, if your association is already loaded, (this.try.loaded? &&
this.try.length) would fire and compute the length from the array. If not,
this.try.count would fire off a SQL COUNT.
Either way, it should already behave exactly like this.try.size. It should
not cause any associations to be loaded.
Can you explain more?
- Scott
Some notes, probably ignroable...
Trying to figure out the intent behind that line of code...
first, check to see if the argument is already numeric: this.try.to_int
works for: Integer, Float, Numeric, BigDecimal
(It also works for Symbol, which is unfortunate:
>> :abc.to_int
=> 158489
Don't pass symbols to the count tag!)
Then, see if you have a will_paginate collection: this.try.total_entries
Then, duplicate the size call: (this.try.loaded? && this.try.length) ||
this.try.count
If the association is loaded, call length, else call count.
works for: ActiveRecord associations
Finally, if count fails, fall back to length: this.try.length
works for: anything else!
Appendix 2: what's the difference between count, length and size anyway?
In http://blog.hasmanythrough.com/2008/2/27/count-length-size Josh says:
Model.count: always runs a SQL COUNT query
Model.length: loads the association if it isn't loaded, then counts the
array
Model.size: like length if the association is loaded, and like count if it
isn't
I concur. count always runs the sql query:
>> c=Channel.first; c.shows.count && c.shows.count && c.shows.count
Channel Load (0.3ms) SELECT * FROM "channels" LIMIT 1
SQL (0.2ms) SELECT count(*) AS count_all FROM "shows" INNER JOIN
"channel_shows" ON "shows".permid_id = "channel_shows".show_id WHERE
(("channel_shows".channel_id = 2147005940))
SQL (0.2ms) SELECT count(*) AS count_all FROM "shows" INNER JOIN
"channel_shows" ON "shows".permid_id = "channel_shows".show_id WHERE
(("channel_shows".channel_id = 2147005940))
SQL (0.2ms) SELECT count(*) AS count_all FROM "shows" INNER JOIN
"channel_shows" ON "shows".permid_id = "channel_shows".show_id WHERE
(("channel_shows".channel_id = 2147005940))
=> 23
length loads the association if it's not already loaded, then returns the
array length:
>> c=Channel.first; c.shows.length && c.shows.length && c.shows.length
Channel Load (0.3ms) SELECT * FROM "channels" LIMIT 1
Show Load (3.6ms) SELECT "shows".* FROM "shows" INNER JOIN "channel_shows"
ON "shows".permid_id = "channel_shows".show_id WHERE
(("channel_shows".channel_id = 2147005940))
=> 23
size acts like count if the association isn't loaded, and like length if it
is:
>> c=Channel.first; c.shows.size && c.shows.size && c.shows.reload &&
c.shows.size && c.shows.size
Channel Load (0.3ms) SELECT * FROM "channels" LIMIT 1
SQL (0.2ms) SELECT count(*) AS count_all FROM "shows" INNER JOIN
"channel_shows" ON "shows".permid_id = "channel_shows".show_id WHERE
(("channel_shows".channel_id = 2147005940))
SQL (0.2ms) SELECT count(*) AS count_all FROM "shows" INNER JOIN
"channel_shows" ON "shows".permid_id = "channel_shows".show_id WHERE
(("channel_shows".channel_id = 2147005940))
Show Load (3.6ms) SELECT "shows".* FROM "shows" INNER JOIN "channel_shows"
ON "shows".permid_id = "channel_shows".show_id WHERE
(("channel_shows".channel_id = 2147005940))
=> 23
On Tue, Aug 11, 2009 at 3:54 PM, kevinpfromnm <[email protected]>wrote:
>
> No, I haven't yet.
>
> On Aug 11, 8:18 am, Owen <[email protected]> wrote:
> > Cool. Have you alerted Bryan or Tom?
> >
> > On Aug 7, 2:02 am, kevinpfromnm <[email protected]> wrote:
> >
> > > I made one small change to a local version of the count tag and it
> > > kept it from loading the whole association every time I used it.
> >
> > > I added this.try.size || to the beginning of the c = line. The
> > > try.to_int was causing it to load the whole association. Size is nice
> > > because if it's already loaded it uses array length. If it's not, it
> > > uses count. And it will use an auto cache counter column if it's
> > > available.
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Hobo
Users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/hobousers?hl=en
-~----------~----~----~----~------~----~------~--~---