Fw: Re: [rug-b] [Rails] Namespacing models

3 views
Skip to first unread message

Pierpaolo Frasa

unread,
Mar 8, 2015, 1:18:14 PM3/8/15
to ru...@googlegroups.com
Forwarded private conversation, since it might be interesting for other people as well.

a) Yes, exactly.
b) I’m not an expert, however, when I last tried it, it worked fine with (the equivalent of) „has_many :bills“, without the explicit class_name from within the same module. FWIW, I had a Frontend::User and an Admin::User class (and table), with respective UserRole classes and it worked. 
c) You do have to set the class name explicitly from without the module. has_many :bills, class_name: Finance::Bill.

At least that’s what worked for me. I suggest you run some tests for it. I know you shouldn’t test the framework, but when your working with edge cases / monkey patches like this...

Unfortunately, I’m not aware of such discussions. It seems to be just one of those things where, when you try to fight Rails conventions, it becomes incredibly messy. :(

HTH
Pierpaolo

Am 3. März 2015 bei 18:22:24, Oliver Thamm (ollid...@googlemail.com) schrieb:

Thanks Pierpaolo! :)

a) You do have a DB table named "finance_bill", right? Sorry I am doublechecking that...

b) From within the Finance module/namespace, I'd expect to define a relation just like `has_many :bills`, right? (Or would I still have to explicitly specify the class_name? And as `Bill` or `Finance::Bill`)

c) From outside the Finance module/namespace, how do I define a relation then? `has_many :finance_bills` ? Or `has_many :bills, class_name: 'Finance::Bill'` ?

Is there any discussion on a convention going on, that you heard of?


Cheers, Oliver


Am 03.03.2015 um 18:00 schrieb Pierpaolo Frasa:
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

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

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

--
--
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.

Reply all
Reply to author
Forward
0 new messages