I dug a little and no, there is no to_int method on the association. When trying to_int it gives the no method exception without loading the association, but when I add the .try to it, it loads the association. Might there be a bug in the try code that triggers association loads under certain circumstances?
On Aug 17, 3:46 pm, kevinpfromnm <[email protected]> wrote: > I don't know, I don't have anything particular defined on either > model. I added a counter_cache after the fact trying to get rid of > the extra loads. > > Here are the models in question > > class Post < ActiveRecord::Base > > hobo_model # Don't put anything above this > > acts_as_taggable > > fields do > title :string, :required > body :markdown > comments_count :integer, :default => 0 > timestamps > end > > set_default_order "created_at desc" > > has_many :comments, :as => :commentable, :dependent > => :destroy, :accessible => true > belongs_to :user, :creator => true > > named_scope :include_tags, { :include => :tags } > named_scope :include_comments, { :include => :comments } > # --- Permissions --- # > ... > end > > class Comment < ActiveRecord::Base > > hobo_model # Don't put anything above this > > fields do > body :text, :required > timestamps > end > > belongs_to :commentable, :polymorphic => true, :counter_cache => > true > belongs_to :owner, :class_name => "User", :creator => true > > has_many :content_reports, :as => :reportable > > named_scope :recent, lambda { |n| { :order => 'updated_at > DESC', :limit => n } } > > set_default_order "created_at desc" > > # --- Permissions --- # > ... > > # other methods > def shortened_body > return body.slice(0,100) + "..." if body.length > 100 > body > end > end > > On Aug 17, 12:31 am, Scott Bronson <[email protected]> wrote: > > > has_many doesn't provide a to_int method. Not sure where your model is > > getting it from. > > > On Sun, Aug 16, 2009 at 2:51 PM, kevinpfromnm <[email protected]>wrote: > > > > I have a polymorphic comments has_many relationship. I was counting > > > the comments. > > > > On Aug 16, 9:49 am, Scott Bronson <[email protected]> wrote: > > > > I don't think any AR collections or relations implement to_int. > > > > > What object were you counting? > > > > > On Fri, Aug 14, 2009 at 3:11 PM, kevinpfromnm <[email protected] > > > >wrote: > > > > > > I don't know but I tested and the to_int was the point it was loading > > > > > the association. I added an || 6 right after it and it still loaded > > > > > the association. Adding the try.size before it avoided it. > > > > > > Rails 2.3.2 > > > > > Edge hobo as of 2aff4f212fb38ec0c7094b47e56a717445fb25e8 > > > > > > On Aug 13, 5:59 pm, Scott Bronson <[email protected]> wrote: > > > >https://hobo.lighthouseapp.com/projects/8324/tickets/261-calling-resp... > > > > > > > Yep, that's probably the most likely culprit. > > > > > > > On Thu, Aug 13, 2009 at 4:40 PM, Matt Jones <[email protected]> > > > wrote: > > > > > > > > There's a old bug that we still haven't pinned down that sometimes > > > > > > > causes extra association loads when calling respond_to? on an > > > > > > > AssociationCollection. I don't have the Lighthouse number handy, > > > but > > > > > > > it's a known issue (possibly with AR itself and not Hobo). > > > > > > > > This may be a related issue. > > > > > > > > --Matt Jones > > > > > > > > On Aug 13, 2009, at 7:14 PM, Scott Bronson wrote: > > > > > > > > > I don't see why thecounttag 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 aCOUNTbut 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.countwould fire off a SQLCOUNT. > > > > > > > > > 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 thecounttag!) > > > > > > > > > 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 callcount. > > > > > > > > works for: ActiveRecord associations > > > > > > > > > Finally, ifcountfails, fall back to length: this.try.length > > > > > > > > works for: anything else! > > > > > > > > > Appendix 2: what's the difference betweencount, length and size > > > > > > > > anyway? > > > > > > > > > Inhttp://blog.hasmanythrough.com/2008/2/27/count-length-sizeJosh > > > > > > > > says: > > > > > > > > Model.count: always runs a SQLCOUNTquery > > > > > > > > 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 > > > > > > > >countif it isn't > > > > > > > > > I concur. countalways 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) SELECTcount(*) 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) SELECTcount(*) 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) SELECTcount(*) 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 likecountif 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) SELECTcount(*) 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) SELECTcount(*) 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 thecounttag > > > 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 > > > > > > > > > > usescount. 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 -~----------~----~----~----~------~----~------~--~---
