[Rails] Namespacing models

13 views
Skip to first unread message

Oliver Thamm

unread,
Mar 3, 2015, 11:52:57 AM3/3/15
to ru...@googlegroups.com
Hey everyone,

if you'd want to put a Rails ActiveRecord Model under a namespace, say:
`class Bill < ActiveRecord::Base` => `class Finance::Bill <
ActiveRecord::Base`, would you rename the DB table, too? Say: `bills` =>
`finance_bills`.

The other option would be to explixitly set the namespaced class name on
every relation, which I'd want to avoid. Is there any dconvention on that?

The interwebs leave me alone on this issue, can you help me out? Any
other ideas?


Cheers, Oliver

Enrico Thierbach

unread,
Mar 3, 2015, 1:34:37 PM3/3/15
to ru...@googlegroups.com
Oliver, there Is set_table_name 
--
--
You received this message because you are subscribed to the Google Groups "RUG-B" group.
To post to this group, send email to ru...@googlegroups.com
To unsubscribe from this group, send email to rug-b-un...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/rug-b?hl=de or http://www.rug-b.de

--- You received this message because you are subscribed to the Google Groups "RUG-B" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rug-b+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Pierpaolo Frasa

unread,
Mar 8, 2015, 1:18:13 PM3/8/15
to ru...@googlegroups.com, Oliver Thamm
Sorry, the code was wrong. It’s

module Finance
  def self.table_name_prefix
    ‚finance_‘
  end
end

Am 3. März 2015 bei 17:59:17, Pierpaolo Frasa (pfr...@gmail.com) schrieb:

Hi,

app/models/finance.rb

module Finance
  def self.table_name_prefix = ‚finance_‘
end

However, due to the way Rails autoloading works, it is sometimes a mess to get this module file loaded every time a nested class is loaded.

I have found that it works when doing
module Finance
  class Bill < ActiveRecord::Base
    …
  end
end

but not with
class Finance::Bill < ActiveRecord::Base
  …
end

Although I can’t say if this behaviour is consistent.

Cheers
Pierpaolo

Tilmann Singer

unread,
Mar 8, 2015, 1:18:14 PM3/8/15
to Oliver Thamm, ru...@googlegroups.com
I'd say choose the path of least resistance and let rails do its
convention over configuration magic, by renaming the table to
finance_bills. Unless there is anything that would make the migration
more painful than usual, such as an outside dependency on the table
name.


Til
signature.asc

Pierpaolo Frasa

unread,
Mar 8, 2015, 1:18:14 PM3/8/15
to ru...@googlegroups.com, Oliver Thamm
Hi,

app/models/finance.rb

module Finance
  def self.table_name_prefix = ‚finance_‘
end

However, due to the way Rails autoloading works, it is sometimes a mess to get this module file loaded every time a nested class is loaded.

I have found that it works when doing
module Finance
  class Bill < ActiveRecord::Base
    …
  end
end

but not with
class Finance::Bill < ActiveRecord::Base
  …
end

Although I can’t say if this behaviour is consistent.

Cheers
Pierpaolo

Am 3. März 2015 bei 17:52:58, Oliver Thamm (ollid...@googlemail.com) schrieb:

Jay Swain

unread,
Mar 8, 2015, 1:18:33 PM3/8/15
to ru...@googlegroups.com
That would be under "single table inheritance" see here: http://api.rubyonrails.org/classes/ActiveRecord/Base.html

Essentially - you would use the bills table, and you would add a type column as a string.

Pierpaolo Frasa

unread,
Mar 8, 2015, 2:14:43 PM3/8/15
to ru...@googlegroups.com
Hi,

I believe there is some misunderstanding about what this discussion is about.

First, we are not talking about inheritance. Finance::Bill still inherits directly from ActiveRecord::Base. (While we're at that, I have my own reservations about single table inheritance in general, but that's another topic.)
Second, we're exactly trying to achieve the mapping from Finance::Bill to the table "finance_bill". However, Rails doesn't do that automatically - it will just use the class name, without considering the module. There are, as far as I can see, essentially two ways to achieve this behaviour:

a) You explicitly define the mapping in every namespaced class via "self.table_name = 'finance_bill'", but this isn't very DRY; or
b) You define the self.table_name_prefix method in the parent module, see my first reply. However, due to Rails autoloading this appears to be somewhat tricky (at least in my experience).

Best,
Pierpaolo
--
Reply all
Reply to author
Forward
0 new messages