Singularize individual table name #1162

Merged
merged 3 commits into from May 21, 2011

Projects

None yet

3 participants

@guilleiguaran
Member

This allow set pluralize_table_names for an individual class.

class Post < ActiveRecord::Base
  self.pluralize_table_names = false
end

Previously when you set it for a class it was used globally (We discuss a little about it here: #560 (comment))

cc @jonleighton

@josevalim
Member

+1

@jonleighton
Member

@tenderlove and I discussed this the other day. We talked about the following situation:

class Animal < AR::Base
end

class Horse < Animal
  self.pluralize_table_names = false
end

Note that Horse and Animal will share the same table under usual circumstances.

@tenderlove said that changing pluralize_table_names on the subclass should affect the superclass in this scenario, since they normally share the same table. I agreed at the time.

However, I now think the opposite. I think that in the above situation Horse and Animal should have the table names animal and animals, because:

  1. If changing the subclass affects the superclass, it is at the very least a code smell to make the change in the subclass
  2. I think we should treat this like self.table_name in the sense of "whatever you would have called this table, call it the singular of that"
  3. It seems more obvious to do it this way, and certainly the implementation will be simpler (the other way would need to deal with abstract classes appropriately as well)

Thoughts? (All this seems quite academic though as I can't envisage the above situation arising in real life.)

@josevalim
Member

Changing a subclass should not affect the superclass. If you want to affect everyone, the configuration option should be applied to Animal.

@jonleighton
Member

@guilleiguaran your patch does not change AliasTracker, which currently checks ActiveRecord::Base.pluralize_table_names, which makes me wonder how test_has_many_with_pluralize_table_names_false still works with this patch. Could you investigate please?

@guilleiguaran
Member

I've changed AliasTracker, take a look to the last commit. I'm investigating but I haven't found how was working without patch.

@jonleighton jonleighton merged commit d62a39a into rails:master May 21, 2011
@jonleighton
Member

Ah, just realised it would have worked before because all it's doing is generating an alias, not an actual table name. Merged.

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