Generating ActiveRecord model table name to include namespace

62 views
Skip to first unread message

Matthew Dunbar

unread,
Jan 31, 2015, 1:52:01 AM1/31/15
to rubyonra...@googlegroups.com
I will implement this myself, looking for feedback on if it'd be accepted and how the community feels about the long term of this being enabled by default.

I often run into conflicts between table names when running multiple fairly heavyweight engines that all deal with e-commerce (and in turn many have tables such as most recently payments that conflict). The current solution is to manually specify the table prefix in the appropriate gem / module.

I am suggesting that we add a inherits_prefix_from_namespace flag to models, then eventually in the long term (perhaps rails 5) defaulting this to be enabled.

Ideally this would work as follows:

module Example
 
module Widgets
   
class Test < ActiveRecord::Base
      inherits_table_prefix_from_namespace
: true
    end
  end
end

Rather than having its table name be tests , the table name would be example_widgets_tests.

In the case that a prefix is specified, the table name would be prefix_example_widgets_tests (rather than prefix_tests).

Carlos Antonio da Silva

unread,
Feb 1, 2015, 7:39:58 AM2/1/15
to rubyonra...@googlegroups.com
Not sure I get what's the actual difference between a new option like that and setting the table name prefix on the module, which will work for all classes under it? Can you expand on that a bit?

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-co...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
Visit this group at http://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.



--
At.
Carlos Antonio

Bráulio Bhavamitra

unread,
Feb 1, 2015, 7:50:04 AM2/1/15
to rubyonra...@googlegroups.com
Check http://apidock.com/rails/ActiveRecord/Base/table_name_prefix/class
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-co...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> Visit this group at http://groups.google.com/group/rubyonrails-core.
> For more options, visit https://groups.google.com/d/optout.
>

--
"Lute pela sua ideologia. Seja um com sua ideologia. Viva pela sua ideologia. Morra por sua ideologia" P.R. Sarkar

EITA - Educação, Informação e Tecnologias para Autogestão
http://cirandas.net/brauliobo
http://eita.org.br

"Paramapurusha é meu pai e Parama Prakriti é minha mãe. O universo é meu lar e todos nós somos cidadãos deste cosmo. Este universo é a imaginação da Mente Macrocósmica, e todas as entidades estão sendo criadas, preservadas e destruídas nas fases de extroversão e introversão do fluxo imaginativo cósmico. No âmbito pessoal, quando uma pessoa imagina algo em sua mente, naquele momento, essa pessoa é a única proprietária daquilo que ela imagina, e ninguém mais. Quando um ser humano criado mentalmente caminha por um milharal também imaginado, a pessoa imaginada não é a propriedade desse milharal, pois ele pertence ao indivíduo que o está imaginando. Este universo foi criado na imaginação de Brahma, a Entidade Suprema, por isso a propriedade deste universo é de Brahma, e não dos microcosmos que também foram criados pela imaginação de Brahma. Nenhuma propriedade deste mundo, mutável ou imutável, pertence a um indivíduo em particular; tudo é o patrimônio comum de todos."
Restante do texto em http://cirandas.net/brauliobo/blog/a-problematica-de-hoje-em-dia

Matthew Dunbar

unread,
Feb 3, 2015, 5:52:13 AM2/3/15
to rubyonra...@googlegroups.com
Suggesting that we can auto generate the prefix based on the module name, rather than setting it with the existing method. Then eventually this feels like a sensible default to me.

Carlos Antonio da Silva

unread,
Feb 3, 2015, 5:58:08 AM2/3/15
to rubyonra...@googlegroups.com
The first suggestion, as far as I understood, was related to something like inherits_table_prefix_from_namespace, which seems pretty much the same as setting the table name prefix.

In any case, I've seen many many cases where people organize their models in namespaces, but the tables might not have the namespace prefix. Making this a default would likely be a considerable breaking change, and I'm leaned to think it would be unnecessary overhead.

Best.

Matt Jones

unread,
Feb 3, 2015, 5:03:44 PM2/3/15
to rubyonra...@googlegroups.com
This will be a pretty rough default for some DBs that have short identifier maximums (Oracle is *30* characters).

We’d also need to be explicit about what happens with models that descend from namespaced classes (from an engine, for instance). IIRC, current behavior derives the table name for subclasses from the direct descendant of AR::Base.

—Matt Jones
signature.asc
Reply all
Reply to author
Forward
0 new messages