This project is archived and is in readonly mode.

#6795 new

config.cache_classes is broken for HABTM relationships and Postgres

Reported by quarterdome | May 22nd, 2011 @ 10:15 PM

Lets say we have a many to many relationship like this:

class Unit

has_and_belongs_to_many :photos


class Photo

has_and_belongs_to_many :units


There is also a join table like this:

create_table "photos_units", :id => false, :force => true do |t|

t.integer "unit_id"
t.integer "photo_id"


Now we launch Rails console and do something like this:

Photo.first.units << Unit.first

Checking the devlopment.log, I see the following queries there:

[DEBUG: 2011/05/22 14:06:21 #16460] PK and serial sequence (1.2ms) SELECT attr.attname, seq.relname FROM pg_class seq, pg_attribute attr, pg_depend dep, pg_namespace name, pg_constraint cons WHERE seq.oid = dep.objid AND seq.relkind = 'S' AND attr.attrelid = dep.refobjid AND attr.attnum = dep.refobjsubid AND attr.attrelid = cons.conrelid AND attr.attnum = cons.conkey[1] AND cons.contype = 'p' AND dep.refobjid = '"photos_units"'::regclass [DEBUG: 2011/05/22 14:06:21 #16460] PK and custom sequence (0.6ms) SELECT attr.attname, CASE WHEN split_part(def.adsrc, '''', 2) ~ '.' THEN substr(split_part(def.adsrc, '''', 2), strpos(split_part(def.adsrc, '''', 2), '.')+1) ELSE split_part(def.adsrc, '''', 2) END FROM pg_class t JOIN pg_attribute attr ON (t.oid = attrelid) JOIN pg_attrdef def ON (adrelid = attrelid AND adnum = attnum) JOIN pg_constraint cons ON (conrelid = adrelid AND adnum = conkey[1]) WHERE t.oid = '"photos_units"'::regclass AND cons.contype = 'p' AND def.adsrc ~* 'nextval'

Note that these column definition queries are executed over and over again. If I keep executing the "Photo.first.units << Unit.first" on console, the column definitions queries keep happening again (with no caching). I do have config.cache_classes set to true.

This is on Postgres 8.3.14 and Rails 3.0.6.

This sounds like a Rails / ActiveRecord bug to me. Any help is greatly appreciated!

No comments found

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>

People watching this ticket