HABTM problem when using count() on the association (it then

Hi all -

Just ran into a strange problem on Rails 1.1.6 (if this is a 1.1.6
issue,
stop me now and let me know :).

I’m running into an odd situation…

model.habtm_association.count => 1234 # this is okay and sql shows a
# COUNT(*) query

However, if I now do model.inspect or save model into memcache, it loads
up all of habtm_association and I’ve confirmed that by looking at the
SQL.
This happens both in the website and in the console (by just typing
‘model’ even).

Any idea why this is happening? Here’s my models…

class GalleryCategory < ActiveRecord::Base

has_and_belongs_to_many :video_gallery_entries,
:join_table => :gallery_categories_gallery_entries,
:association_foreign_key => :gallery_entry_id,
:conditions => “gallery_entries.enabled = true AND
gallery_entries.type = ‘VideoGalleryEntry’”,
:order => ‘gallery_entries.created_at DESC’,
:uniq => true

end

class GalleryEntry < ActiveRecord::Base

has_and_belongs_to_many :gallery_categories,
:join_table => :gallery_categories_gallery_entries,
:foreign_key => :gallery_entry_id,
:order => ‘gallery_categories.name’,
:uniq => true

end

class VideoGalleryEntry < GalleryEntry
end

The steps to cause this problem along with the SQL generated are:

c = GalleryCategory.find(:first)=> #<GalleryCategory:0x326bb48
@attributes={“name”=>“Games and Gadgets”, “id”=>“5”, “blurb”=>""}>

SELECT * FROM gallery_categories LIMIT 1

c.video_gallery_entries.count
=> 1

SELECT count(*) AS count_all FROM gallery_entries INNER JOIN
gallery_categories_gallery_entries ON gallery_entries.id =
gallery_categories_gallery_entries.gallery_entry_id WHERE
(gallery_categories_gallery_entries.gallery_category_id = 5 AND
(gallery_entries.enabled = true AND gallery_entries.type =
‘VideoGalleryEntry’)) AND ( (gallery_entries.type =
‘VideoGalleryEntry’ ) )

(this is where things get weird… below I just type ‘c’, but within my
website if I do c.inspect or save c into memcache the same thing
happens)

c
=> #<GalleryCategory:0x326bb48 @attributes={“name”=>“Games and Gadgets”,
“id”=>“5”, “blurb”=>""},
@video_gallery_entries=[#<VideoGalleryEntry:0x31c7250
@attributes={“event_id”=>“0”, “updated_at”=>“2007-02-13 02:30:34”,
“featured”=>“1”, “event_day_id”=>“0”, “gallery_category_id”=>“5”,
“type”=>“VideoGalleryEntry”, “tournament_location_id”=>nil,
“enabled”=>“1”, “id”=>“22840”, “tournament_id”=>“0”, “position”=>“10”,
“gallery_entry_id”=>“22840”, “created_at”=>“2007-02-12 06:25:06”}>]>

SELECT * FROM gallery_entries INNER JOIN
gallery_categories_gallery_entries ON gallery_entries.id =
gallery_categories_gallery_entries.gallery_entry_id WHERE
(gallery_categories_gallery_entries.gallery_category_id = 5 AND
(gallery_entries.enabled = true AND gallery_entries.type =
‘VideoGalleryEntry’)) AND ( (gallery_entries.type =
‘VideoGalleryEntry’ ) ) ORDER BY gallery_entries.created_at DESC

Can someone shed some light on why this is happening? I’m thinking that
by calling count() I’ve created a stub for the association so when I
inspect it or save it memcache it sees that and figures it needs to load
it up since it isn’t already?

Thanks for any pointers!

-philip

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs