Forum: Ruby on Rails 2 Models: Same name, different namespace => Problems

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Florian D. (Guest)
on 2008-10-16 13:27
Attachment: 2_models_test.zip (0 Bytes)
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 %>
<hr/>
<% 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
Frederick C. (Guest)
on 2008-10-16 13:38
(Received via mailing list)
On Oct 16, 10:27 am, Florian Dütsch <removed_email_address@domain.invalid>
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/br...)
You may be able to sidestep this by just using require_dependency to
require stuff ahead of time

Fred
Florian D. (Guest)
on 2008-10-16 15:17
Frederick C. wrote:
> On Oct 16, 10:27�am, Florian D�tsch <removed_email_address@domain.invalid>
> 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/br...)
> 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. (Guest)
on 2008-10-16 18:40
(Received via mailing list)
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
Florian D. (Guest)
on 2008-10-17 11:28
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
Frederick C. (Guest)
on 2008-10-17 12:04
(Received via mailing list)
On Oct 17, 8:28 am, Florian Dütsch <removed_email_address@domain.invalid>
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
This topic is locked and can not be replied to.