2 Models: Same name, different namespace => Problems

Let’s start with an example:


class Comment < ActiveRecord::Base
end

dummy (to invent a “table namespace”)

class Review < ActiveRecord::Base
end

… dummy

is “review_comments” in the database

class Review::Comment < ActiveRecord::Base
belongs_to :thing
end

class Review::Thing < ActiveRecord::Base
has_many :comments, :class_name => ‘::Review::Comment’, :foreign_key
=> ‘thing_id’
end

Then we use the models in a view:
(I know it’s not clean to use the models directly in the view, but it is
simply an example

<% Comment.find(:all).each do |c| %>
<%= c.created_at %>
<% end %>



<% Review::Thing.find(:first).comments.each do |c| %>
<%= c.created_at %>
<% end %>

The following error arises:
SQLite3::SQLException: no such column: comments.thing_id: SELECT * FROM
“comments” WHERE (“comments”.thing_id = 1)

It happens because
“ActiveRecord::Base.compute_type(’::Review::Comment’)” returns class
“Comment” instead of class “Review::Comment”.

Additionally the following warning arises in the logs:
…/vendor/rails/activerecord/lib/active_record/base.rb:1910: warning:
toplevel constant Comment referenced by Review::Comment

Ok, so class_eval("::Review::Comment") in “compute_type” returns the
toplevel constant “Comment”, the wrong class!

Does anybody know why?
Is it a bug?

I attached a test project.
Thanks for your opinions!

der Flo

On Oct 16, 10:27 am, Florian Dütsch [email protected]
wrote:

Does anybody know why?
Is it a bug?

There’s some funkyness to do with the automatic loading and namespaces
(see
http://groups.google.com/group/rubyonrails-core/browse_thread/thread/17e9e5e564e8ea64/def2cec288984fa3?lnk=gst&q=const_missing#def2cec288984fa3)
You may be able to sidestep this by just using require_dependency to
require stuff ahead of time

Fred

On 16 Oct 2008, at 12:17, Florian Dütsch wrote:

(see

the whole const_missing autoloading thing has a number of quirks.
Namespaced models aren’t used very often, at least in the past there
have been a number of edge cases.

Fred

Frederick C. wrote:

On Oct 16, 10:27�am, Florian D�tsch [email protected]
wrote:

Does anybody know why?
Is it a bug?

There’s some funkyness to do with the automatic loading and namespaces
(see
http://groups.google.com/group/rubyonrails-core/browse_thread/thread/17e9e5e564e8ea64/def2cec288984fa3?lnk=gst&q=const_missing#def2cec288984fa3)
You may be able to sidestep this by just using require_dependency to
require stuff ahead of time

Fred
Hi Fred!

A “require_dependency ‘review/comment’” on review/thing.rb solved the
problem! Do you agree when I say it is a hack?
Is ActiveRecord’s namespace support as bad?

Thanks,
der Flo

Frederick C. wrote:

On 16 Oct 2008, at 12:17, Florian Dütsch wrote:

(see

the whole const_missing autoloading thing has a number of quirks.
Namespaced models aren’t used very often, at least in the past there
have been a number of edge cases.

Fred

Thanks Fred!

Should I open a feature request at http://rails.lighthouseapp.com or
does the rails core team provide a possibilty to vote for new features?

Bye,
der Flo

On Oct 17, 8:28 am, Florian Dütsch [email protected]
wrote:

Thanks Fred!

Should I open a feature request athttp://rails.lighthouseapp.comor
does the rails core team provide a possibilty to vote for new features?

You might want to post to the rubyonrails-core google group. Although
like I said, last time this was discussed it wasn’t clear what to do
about it.

Fred