Skip to content

Instantly share code, notes, and snippets.

@jm
Created January 23, 2012 20:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jm/1665503 to your computer and use it in GitHub Desktop.
Save jm/1665503 to your computer and use it in GitHub Desktop.
# Let's say I have two AR models...
class Cabinet < ActiveRecord::Base
has_many :files, :class_name => "CabinetFile"
end
class CabinetFile < ActiveRecord::Base
belongs_to :cabinet, :inverse_of => :files, :counter_cache => true
end
# Now, I would ASSUME that the counter cache column would be "files_count" since
# I have told it "Yo, ActiveRecord, the association target is, in fact, named files."
# But, if I create a CabinetFile like this sort of...
CabinetFile.create(:body => "winning!", :cabinet => @cabinet)
# ...I get an error about it not finding the cache column "cabinet_files_count". I've
# isolated the code and have a patch I think, but wanted to make sure it was worth even
# submitting or if I'm just wrong about how that's supposed work...
@gilesbowkett
Copy link

there's a Destroy All Software episode where Gary Bernhardt shows code which he calls an oversimplified manual rebuild of has_many :through, because he only needed a simple subset of the functionality and it was less hassle than trying to remember how has_many :through worked. obviously not a best practice, but Gary's a very smart guy, so seeing him come out of the "ActiveRecord wtf is going on in there?" closet makes me brave enough to do the same thing.

personally I've even written specs to document and clarify ActiveRecord features in apps before, despite knowing for an absolute fact that the ActiveRecord features in question worked perfectly well and were already tested within the framework itself.

my point is just that there's a lot of stuff in ActiveRecord where the APIs could be a lot clearer -- in my personal opinion, anything in ActiveRecord which wasn't written by DHH himself before 2006 is going to be idiosyncratic at best and flat-out baffling at worst -- so any patch which aims to make it easier to read or use is a good idea in my opinion. find and save and the whole ActiveRelation chaining thing are incredibly clear and a joy to use, but a lot of the other stuff just short-circuits my brain until all I can do is make cow noises.

tangent, but I think the weird "you have to remember it" status for the differences in new vs create or edit vs update in Rails controllers is also a bit of API cruft which could do with tidying up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment